可视 VS2008 C++ "interface"作为参数名称编译失败

visual VS2008 C++ "interface" as a parameter name fails to compile

本文关键字:编译 失败 参数 VS2008 C++ interface 可视      更新时间:2023-10-16

正如标题所说,我在VS2008 C++程序中遇到编译器错误。 我不确定如何比在代码中更好地描述我的问题。 除非我取消注释 TEST 行,否则以下内容将编译。

#include <windows.h>
#include <iostream>
using namespace std;
//#define TEST //<-- uncomment for error
#ifdef TEST
void test(void* interface)
{
    return;
}
#endif
int main()
{
    cout << "Hello World" << endl;
    system("PAUSE");
    return(0);
}

取消注释时,我收到以下错误:

1>main.cpp(7) : error C2332: 'struct' : missing tag name
1>main.cpp(7) : error C2144: syntax error : '<unnamed-tag>' should be preceded by ')'
1>main.cpp(7) : error C2144: syntax error : '<unnamed-tag>' should be preceded by ';'
1>main.cpp(7) : error C2059: syntax error : ')'
1>main.cpp(8) : warning C4094: untagged 'struct' declared no symbols
1>main.cpp(8) : error C2143: syntax error : missing ';' before '{'
1>main.cpp(8) : error C2447: '{' : missing function header (old-style formal list?)

这是非托管代码,所以我不确定接口这个词有什么问题。 有没有办法让这段代码按原样编译,还是我必须将术语接口的每个实例都更改为其他内容?

谢谢!

如果你

的代码需要包含Windows.h那么你应该避免使用名称interface,因为它是为Windows SDK保留的使用而保留的(本质上它是关键字struct的同义词(。 可能有技巧可以解决这个问题(您可以在包含 SDK 标头后#undef interface(,但您可能应该避免使用该标识符。

单词

interface由MSVC++保留,因为它是Microsoft编译器添加的非标准keyword,用于定义MSVC++中的接口

因此,请

为参数使用不同的名称,如下所示:

#ifdef TEST
void test(void* test_interface)
{
    return;
}
#endif