【问题标题】:std::complex<float> with error C2106: '=': left operand must be l-value带有错误 C2106 的 std::complex<float>:'=':左操作数必须是左值
【发布时间】:2022-01-10 07:27:22
【问题描述】:

我对我的signal 执行了快速傅立叶变换 (fft),将其转换为 signalComplexsignal 是一系列实数浮点数,signalComplex 代表一系列复数:

std::vector<std::complex<float>> signalComplex(numSamplesPerScan);   // int numSamplesPerScan

    fft.fwd(signalComplex, signal);    //  std::vector<float> signal

    for (int n = 1; n < numSamplesPerScan / 2; n++)   // simplified procedure to calculate HT
    {
        float real = signalComplex[n].real();   // positive frequency X 2
        float imag = signalComplex[n].imag();

        real *= 2;
        imag *= 2;

        signalComplex[n].real() = real;  // compiler complains here
        signalComplex[n].imag() = imag;  // compiler complains here

        signalComplex[n + numSamplesPerScan / 2].real() = 0;    // compiler complains here
        signalComplex[n + numSamplesPerScan / 2].imag() = 0;    // compiler complains here
    }

在上面的 for 循环中,我试图对 signalComplex 的实部和虚部进行一些简单的计算。但是,编译器会抱怨“error C2106: '=': left operand must be l-value”;在这种情况下,我不确定如何对实部虚部进行计算。

任何指针表示赞赏。一些代码非常感谢。

【问题讨论】:

  • real()imag() 按值返回浮点数,而不是对内部部分的引用。
  • @Brian 谢谢布赖恩。有用。你想发布我更喜欢的答案吗?或者我可以发布我所做的。
  • 查看en.cppreference.com/w/cpp/numeric/complex 面向数组的访问。复数有一项特殊规定,允许直接访问,否则将是 UB。
  • 简化!您是否需要矢量和所有背景信息才能获得编译错误?将你的复合表达式分解成更简单的部分,如auto x = signalComplex[n]; x.real() = real; 现在错误出现在x.real() =real; 行上,推断x 的类型并删除其余代码:int main() { std::complex&lt;float&gt; x; x.real() = real; }。更容易推理,不是吗?

标签: c++ vector stl complex-numbers


【解决方案1】:

我要感谢这里所有试图帮助我的人。

就像布赖恩指出的那样,问题中的代码被修改为

std::vector<std::complex<float>> signalComplex(numSamplesPerScan);

fft.fwd(signalComplex, signal);

for (int n = 1; n < numSamplesPerScan / 2; n++)   
{
    signalComplex[n] = { signalComplex[n].real()*2, signalComplex[n].imag()*2 };  // following is the key part
    signalComplex[n + numSamplesPerScan / 2] = {0, 0};
}

它编译得很好。

【讨论】:

    猜你喜欢
    • 2013-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多