错误:':'令牌之前期望的初始化项
error: expected initializer before ‘:’ token
我正试图用g++-4.4
编译一些c++代码(可以在Windows上使用Visual Studio 2012编译)。
const std::string cnw::restoreSession(const std::vector<string> &inNwsFile) {
for (std::string &nwFile : inNwsFile){
// some...
}
}
,我不能编译,因为这个错误:
CNWController.cpp:154: error: expected initializer before ‘:’ token
你能就如何解决这个问题给我一些建议吗? 您的编译器太旧,无法支持基于范围的for
语法。根据GNU的说法,它最初是在GCC 4.6中支持的。GCC还要求您显式地请求c++ 11支持,通过在与您一样老的编译器上提供-std=c++11
或c++0x
命令行选项。
如果你不能升级,那么你需要旧的等效:
for (auto it = inNwsFile.begin(); it != inNwsFile.end(); ++it) {
std::string const &nwFile = *it; // const needed because inNwsFile is const
//some...
}
我相信auto
在GCC 4.4中是可用的(只要你启用c++ 0x支持),以节省你编写std::vector<string>::const_iterator
。
如果你确实需要一个非const
的引用来指向vector的元素,那么,无论你使用哪种类型的循环,你都需要从函数形参中删除const
。
相关文章:
- 是否可以初始化不可复制类型的成员变量(或基类)
- C++使用整数的压缩数组初始化对象
- C++初始化基类
- 多成员Constexpr结构初始化
- 复制列表初始化的隐式转换的等级是多少
- 内联映射初始化的动态atexit析构函数崩溃
- 如何在C++中初始化嵌套类中的2个memeber
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 没有用于初始化C++中的变量模板的匹配构造函数
- 在未初始化映射的情况下,将值插入到映射的映射中
- C++成员初始化
- 为什么在C++中首先初始化成员类
- 同时具有"聚合初始化"和"模板推导"
- 初始化具有非默认构造函数的std::数组项的更好方法
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 在C和C++中初始化结构中的数组
- 如何初始化一个在C++中期望向量和变量的构造函数<int>?
- 错误:':'令牌之前期望的初始化项
- 创建的对象总是未定义,并且在初始化构造函数时期望有标识符