C++ 中的 "expected unqualified-id" 错误是什么?
What is "expected unqualified-id" error in C++?
我正在努力学习 stl:bitmap,但我收到以下错误:
Headers 已添加
- 位集
- 字符串
我已经在其他 SO 帖子中搜索过这个错误,但它们与 bitset 无关。
我的代码
int main(){
bitset<size> bs;
bitset<10> bs2(45);
bitset<13> bs3("1100101");
cout << "bs: " << bs << endl;
cout << "bs1: " << bs2 << endl;
cout << "bs2: " << bs3 << endl;
cout << endl;
cout << "bs has " << bs.count() << " set bits" << endl;
cout << bs.size() << endl;
cout << bs2.size() << endl;
cout << bs3.size() << endl;
}
我的错误:最后 3 个 cout 语句出错。
$ g++ test.cpp
test.cpp:28:16: error: expected unqualified-id
cout << bs.size() << endl;
^
test.cpp:6:14: note: expanded from macro 'size'
#define size 16
^
test.cpp:29:17: error: expected unqualified-id
cout << bs2.size() << endl;
^
test.cpp:6:14: note: expanded from macro 'size'
#define size 16
^
test.cpp:30:17: error: expected unqualified-id
cout << bs3.size() << endl;
^
test.cpp:6:14: note: expanded from macro 'size'
#define size 16
^
3 errors generated.
$
从您的程序中删除 #define size 16
。我猜你已经在程序的顶部写了这一行。
您定义的 size
宏与 size()
成员函数冲突。使用 const
变量而不是宏。你应该使用 const int size=16;
您似乎在 test.cpp
第 6 行中定义了一个宏,该宏是替换您尝试调用函数 size
.
的字符串
你的台词实际上是在说:
cout << bs.16() << endl;
cout << bs2.16() << endl;
cout << bs3.16() << endl;
如果您想使用宏,最好使它们尽可能具有描述性,并使用 ALL_UPPER_CASE
来避免此类问题。
例如
#define BITSET_DEFAULT_SIZE 16
编译器给你的错误描述非常详细,让你知道一个宏是这个问题的原因:
test.cpp:28:16: error: expected unqualified-id
cout << bs.size() << endl; <- this is telling you the starting position of the error
^
test.cpp:6:14: note: expanded from macro 'size'
#define size 16 <- this is telling you a macro is involved, and giving its value
此外,由于 std
包含如此多的通用命名函数,因此在您的程序中使用 using namespace std
并不是一个好习惯。例如,如果您创建一个名为 size
的函数,您会突然覆盖 std::size
.
Here is a good post pointing out why this is a bad idea
使用
#undef size
紧跟在行之后
bitset<size> bs;
这将隐藏您的宏,其余代码现在应该可以编译了。
注意:这不是永久修复。但如果宏位于包含在许多文件中的头文件中,这将提供临时修复。但是建议在 C++ 中使用 const 而不是宏。
我正在努力学习 stl:bitmap,但我收到以下错误: Headers 已添加 - 位集 - 字符串
我已经在其他 SO 帖子中搜索过这个错误,但它们与 bitset 无关。
我的代码
int main(){
bitset<size> bs;
bitset<10> bs2(45);
bitset<13> bs3("1100101");
cout << "bs: " << bs << endl;
cout << "bs1: " << bs2 << endl;
cout << "bs2: " << bs3 << endl;
cout << endl;
cout << "bs has " << bs.count() << " set bits" << endl;
cout << bs.size() << endl;
cout << bs2.size() << endl;
cout << bs3.size() << endl;
}
我的错误:最后 3 个 cout 语句出错。
$ g++ test.cpp
test.cpp:28:16: error: expected unqualified-id
cout << bs.size() << endl;
^
test.cpp:6:14: note: expanded from macro 'size'
#define size 16
^
test.cpp:29:17: error: expected unqualified-id
cout << bs2.size() << endl;
^
test.cpp:6:14: note: expanded from macro 'size'
#define size 16
^
test.cpp:30:17: error: expected unqualified-id
cout << bs3.size() << endl;
^
test.cpp:6:14: note: expanded from macro 'size'
#define size 16
^
3 errors generated.
$
从您的程序中删除 #define size 16
。我猜你已经在程序的顶部写了这一行。
size
宏与 size()
成员函数冲突。使用 const
变量而不是宏。你应该使用 const int size=16;
您似乎在 test.cpp
第 6 行中定义了一个宏,该宏是替换您尝试调用函数 size
.
你的台词实际上是在说:
cout << bs.16() << endl;
cout << bs2.16() << endl;
cout << bs3.16() << endl;
如果您想使用宏,最好使它们尽可能具有描述性,并使用 ALL_UPPER_CASE
来避免此类问题。
例如
#define BITSET_DEFAULT_SIZE 16
编译器给你的错误描述非常详细,让你知道一个宏是这个问题的原因:
test.cpp:28:16: error: expected unqualified-id
cout << bs.size() << endl; <- this is telling you the starting position of the error
^
test.cpp:6:14: note: expanded from macro 'size'
#define size 16 <- this is telling you a macro is involved, and giving its value
此外,由于 std
包含如此多的通用命名函数,因此在您的程序中使用 using namespace std
并不是一个好习惯。例如,如果您创建一个名为 size
的函数,您会突然覆盖 std::size
.
Here is a good post pointing out why this is a bad idea
使用
#undef size
紧跟在行之后
bitset<size> bs;
这将隐藏您的宏,其余代码现在应该可以编译了。
注意:这不是永久修复。但如果宏位于包含在许多文件中的头文件中,这将提供临时修复。但是建议在 C++ 中使用 const 而不是宏。