使用 g++ 5.4 编译 C++11
Compiling C++11 with g++ 5.4
编译
时似乎忽略了-std=c++11
:
g++ -std=c++11 -I../include -I ../../../Toolbox/CShmRingBuf/ -I$MILDIR/include CFrameProd.cpp -o CFrameProd.o
CFrameProd.cpp: In constructor ‘CFrameProd::CFrameProd()’:
CFrameProd.cpp:33:24: error: assigning to an array from an initializer list
MilGrabBufferList_ = {0};
我试了-std=c++0x, -std=gnu++0x, -std=c++14
,没有任何帮助。
这是我的 g++ 版本:
g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
我怎样才能让它工作?
下面的代码重现了原始错误:
class CFrameProd{
public:
CFrameProd(){
MilGrabBufferList_ = {0};
}
private:
long MilGrabBufferList_[10];
};
4:28: error: assigning to an array from an initializer list
MilGrabBufferList_ = {0};
但是,此代码编译时没有错误:
class CFrameProd{
public:
CFrameProd(){}
private:
long MilGrabBufferList_[10]={0};
};
此处使用类成员初始化。
发生原始错误的原因是无法在声明数组后将其分配给数组。
(始终可以选择使用初始化器列表:CFrameProd(): MilGrabBufferList_{0}{}
代码似乎在数组声明后将初始值设定项列表分配给数组地址,如下所示:
int main()
{
long int foo[5];
foo = {0};
}
产生错误:assigning to an array from an initializer list
相反,它应该是这样的:
int main()
{
long int foo[5] = {0};
}
在您的情况下,它将是:long MilGrabBufferList_[10] = {0};
您不能在任何版本的C++中分配给数组。
你的问题不是编译器标志,而是你的代码。
相关文章:
- 如何在 atom 中编译 c++11
- 从远程主机中编译C 11代码的问题
- 为什么此代码编译 (C++11) 而没有类型不匹配错误
- 使用 g++ 5.4 编译 C++11
- 在Visual Studio代码中编译C++11
- 为什么"bool c = nullptr ;"编译 (C++11)?
- 如何使用 Orwell Dev-C++ 编译 C++11 代码
- 错误在Windows上使用Clang编译C 11程序
- 尝试在Linux上使用Clang++编译c++11 regex教程时出错
- 如何在Ubuntu上使用Eclipse编译C++11
- 使用 g++ 编译 C++11
- 如何更新 clang 版本以编译 C++11 项目?(在 Mac 上)
- g++错误,当试图用Rcpp编译c++11时
- 如何让g++使用move构造函数编译c++11代码
- 我如何在Ubuntu中编译c++ 11标准
- 在VS2010中编译c++ 11中的代码时,std::function()是否检查类型
- 为较旧的Linux编译c++ 11程序
- 使用-Wpedantic选项编译c++ 11:是否有一个选项可以只禁用关于未命名结构的警告
- 用Clang编译c++11代码的不完整类型
- 在CentOS for Windows上交叉编译c++ 11/14