C++ 带有complex.h的gcc编译器选项

C++ 带有complex.h的gcc编译器选项,c++,gcc,C++,Gcc,我用代码块(使用gcc)编译了一个程序,它运行得很好。 现在我尝试从命令行使用gcc编译相同的程序。它产生了以下错误: error: lvalue required as left operand of assignment 对于这四行: OutArray[Index[g]].real() = TempVBF.FirstReal[g]; OutArray[Index[g]].imag() = TempVBF.FirstImag[g]; OutArray[Index[g]+ElementSize

我用代码块(使用gcc)编译了一个程序,它运行得很好。 现在我尝试从命令行使用gcc编译相同的程序。它产生了以下错误:

error: lvalue required as left operand of assignment
对于这四行:

OutArray[Index[g]].real() = TempVBF.FirstReal[g];
OutArray[Index[g]].imag() = TempVBF.FirstImag[g];
OutArray[Index[g]+ElementSize].real() = TempVBF.SecondReal[g];
OutArray[Index[g]+ElementSize].imag() = TempVBF.SecondImag[g];
这行有四个值,包含两个复数的实部和虚部。我把这些值赋给两个复杂的变量。 我试着这样做:

OutArray[Index[g]] = (TempVBF.FirstReal[g],TempVBF.FirstImag[g]); 
double real() const { return this->_real; }
double& real() { return this->_real; }
OutArray[Index[g]] = MyComplexNumberClass(TempVBF.FirstReal[g],TempVBF.FirstImag[g]);
已编译但在运行时生成错误的值

为什么它先编译,而不是现在??我可以使用什么选项来绕过这个问题而不改变这些行

我正在使用命令:

gcc lib/Globals.cpp lib/Comp/SNT_FFT_Comp.cpp lib/Comp/ST_FFT_Comp.cpp lib/Comp/VNT_FFT_Comp.cpp lib/Comp/VT_FFT_Comp.cpp lib/Decomp/* test/main.cpp -lpthread -std=gnu++0x -o TEST

删除
-std=gnu++0x
会产生更多的错误。将其更改为
-std=c++0x
不会更改任何内容。

您看到的错误几乎肯定是因为
real()
imag()
返回实部或虚部的值;没有提到它。如果没有看到代码,就不可能确定,但更可能是这样定义的:

OutArray[Index[g]] = (TempVBF.FirstReal[g],TempVBF.FirstImag[g]); 
double real() const { return this->_real; }
double& real() { return this->_real; }
OutArray[Index[g]] = MyComplexNumberClass(TempVBF.FirstReal[g],TempVBF.FirstImag[g]);
而不是像这样:

OutArray[Index[g]] = (TempVBF.FirstReal[g],TempVBF.FirstImag[g]); 
double real() const { return this->_real; }
double& real() { return this->_real; }
OutArray[Index[g]] = MyComplexNumberClass(TempVBF.FirstReal[g],TempVBF.FirstImag[g]);
对于后一种情况,可以使用引用执行赋值。对于前一种情况,你有一个复数的真实部分的副本,你不能用它来改变你的复数。如果你愿意,你可以

至于为什么它一开始不起作用,这几乎是不可能回答的。您提供的代码似乎缺少一个类型——换句话说,您应该这样做:

OutArray[Index[g]] = (TempVBF.FirstReal[g],TempVBF.FirstImag[g]); 
double real() const { return this->_real; }
double& real() { return this->_real; }
OutArray[Index[g]] = MyComplexNumberClass(TempVBF.FirstReal[g],TempVBF.FirstImag[g]);
为什么这些值是错误的,可能是由于很多原因造成的(内存中断、索引“g”错误、复数没有您认为它们应该具有的值,等等)。要调试它,您可以尝试一步一步地打印内容的值,直到看到您不希望看到的内容,可以使用
std::cerr