crazy_prog
crazy_prog

Reputation: 1099

Why is compiling this code producing an error?

I believe this is the right header:

  #include <cstdio>

Note, there is a difference between the above declaration and this one:

  #include <stdio.h>

The first one puts everything in the "std" namespace, the 2nd one doesn't. So I am using the first one.

Below is the code which I am compiling using g++4.4.6 on aix6.1:-

#include <cstdarg> //< va_list
#include <cstdio>  //< vsnprintf()
#include "virtual_utils.h"

namespace VS
{


const char* format_str( const char* str, ... ) throw()
{
  static char buf[10][1024];
  static unsigned long buf_no = 0;

  char* cur_buf = buf[ ++buf_no % 10 ];
  buf_no %= 10;

  va_list vl;
  va_start( vl, str );
#ifdef _MSC_VER
  std::_vsnprintf( cur_buf, sizeof(buf), str, vl );
#else
  std::vsnprintf( cur_buf, sizeof(buf), str, vl );
#endif

  return cur_buf;
}


} //< namespace VS

These are the following errors which I am getting:-

virtual_utils.C: In function 'const char* VS::format_str(const char*, ...)':
virtual_utils.C:28: error: 'vsnprintf' is not a member of 'std'

Edit: Modifying the above code to remove the #include "virtual_utils.h" and to add a main(), it compiles with a warning under gcc4.3.4 on Ideone and cleanly under gcc4.5.1.

Upvotes: 4

Views: 827

Answers (1)

ams
ams

Reputation: 25579

Compile with --save-temps, and examine the .ii file it produces. That should make it clear what's defined in what namespace, and what isn't.

Upvotes: 1

Related Questions