使用 g++ 5.4 编译 C++11

Compiling C++11 with g++ 5.4

本文关键字:编译 C++11 g++ 使用      更新时间:2023-10-16
编译

时似乎忽略了-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++中分配给数组。

你的

问题不是编译器标志,而是你的代码。