C++ 编译错误:意外的类型名称"字符串":预期的表达式
c++ compile error: unexpected type name 'string': expected expression
我想使用 c++ stl list 并使用迭代器打印所有元素。这是代码:
#include<list>
#include<algorithm>
#include<string>
using namespace std;
int main(int argc, char* argv[]){
list<string> list;
//list<double> list_double(6);
//list<int> list_int(6, 0);
//list<double> list_double2(6, 0,0);
//list<int> else_list(list_int);
//list<double> iter(list_double.begin(), list_double.end());
list.push_front("1 jack");
list.push_front("2 jackson");
list.push_front("3 sally");
list<string>::iterator itrr;
for (itrr = list.begin(); itrr!= list.end(); itrr++){
string temp = *itrr;
print(temp)nt main(int argc, char* argv[]){
list<string> list;
//list<double> list_double(6);
//list<int> list_int(6, 0);
//list<double> list_double2(6, 0,0);
//list<int> else_list(list_int);
//list<double> iter(list_double.begin(), list_double.end());
list.push_front("1 jack");
list.push_front("2 jackson");
list.push_front("3 sally");
list<string>::iterator itrr;
for (itrr = list.begin(); itrr!= list.end(); itrr++){
string temp = *itrr;
print(temp);
}
return 0;
}
}
return 0;
}
当我尝试编译它时,它显示一些错误:
list.cpp:17:7: error: unexpected type name 'string': expected expression
list<string>::iterator itrr;
^
list.cpp:17:16: error: cannot refer to class template 'iterator' without a template argument list
list<string>::iterator itrr;
~~^
/Library/Developer/CommandLineTools/usr/include/c++/v1/iterator:522:29: note: template is declared here
struct _LIBCPP_TEMPLATE_VIS iterator
^
list.cpp:18:7: error: use of undeclared identifier 'itrr'
for (itrr = list.begin(); itrr!= list.end(); itrr++){
^
list.cpp:18:28: error: use of undeclared identifier 'itrr'
for (itrr = list.begin(); itrr!= list.end(); itrr++){
^
list.cpp:18:47: error: use of undeclared identifier 'itrr'
for (itrr = list.begin(); itrr!= list.end(); itrr++){
^
list.cpp:19:18: error: use of undeclared identifier 'itrr'
string temp = *itrr;
^
那么出了什么问题呢?谢谢!
问题
list<string> list;
将标识符list
定义为类型list<string>
的变量。这将替换以前将标识符list
定义为标准库std::list
类。这意味着当编译器在代码的许多后续点达到list<string>
时,list<string>
毫无意义。<string>
与您可以对变量执行的任何操作都不匹配。
溶液
注意如何重用标识符。制作名为list
的list
会给读者和编译器带来混淆,所以不要这样做。为变量指定不同的名称。它充满了名字,所以为什么不namelist?
这个问题与using namespace std;
的危险相吻合。有关此内容的更多信息,请阅读为什么"使用命名空间 std"被认为是不好的做法?
相关文章:
- 使用正则表达式regex_search在字符串中查找字符串
- C++ 编译错误:意外的类型名称"字符串":预期的表达式
- 如何在C++中使用正则表达式"|"后选择字符串?
- 生成 constexpr 字符串表,不能产生常量表达式
- 如何在 c++11 中使用正则表达式找到确切的子字符串?
- 如何编写将验证以下字符串的正则表达式?
- C++带有捕获组的正则表达式字符串文本
- 如何使用正则表达式从字符串中提取值
- 计算表达式字符串由 std::map 中的键组成
- 在C++中从后缀表达式字符串打印单个操作
- 标记表达式字符串
- C++:表达式:字符串下标超出范围
- C++ 错误:第 1440 行 表达式:字符串下标超出范围
- 给定一个算术表达式字符串,找到表达式的十进制值
- 错误:表达式:字符串下标超出范围
- 调试断言失败:表达式:字符串下标超出范围
- 处理正则表达式字符串时编译崩溃(内存泄漏)
- c++表达式:字符串下标超出范围
- 正则表达式字符串过滤 - 如何允许“-”符号
- 表达式:字符串下标超出范围