2011-11-22 129 views
4

我相信这是正确的标题:为什么编译此代码会产生错误?

#include <cstdio> 

注意,就是有一个以上的声明之间的区别:

#include <stdio.h> 

第一个把一切都在“STD”命名空间,第二个不是。所以我正在使用第一个。

下面是我在AIX6.1使用G ++ 4.4.6编译代码: -

#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 

这是下面的错误这我得到: -

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

编辑: 修改上面的代码以除去#include "virtual_utils.h"和添加main(),它compiles with a warning under gcc4.3.4 on Ideone一个nd cleanly under gcc4.5.1

+3

你确定_MSC_VER没有在任何地方定义? – nos

+1

@nos它是vsnprintf而不是_vsnprintf,_MSC_VER没有在任何地方定义。 –

+2

'std :: vsnprintf'在''而不是'' – CharlesB

回答

1

编译--save-temps,并检查它产生的.ii文件。这应该清楚什么是在什么名称空间中定义的,什么不是。

+0

错误已知 - “virtual_utils.C:28:错误:'vsnprintf'不是'std'”的成员。 –

+0

错误是已知的,原因不是,解决方案不是。这将向您展示编译器看到的内容,并且您可以从中了解如何处理它。 – ams

+0

我们知道vsnprintf不是std的成员。它用gcc-4.0.2/aix编译好了吗?我们现在能做什么? –

相关问题