2017-08-25 91 views
1

我需要访问C++中复杂数据矢量的特定元素。使用复杂矢量的错误

以下是我有:

vector< complex<float> > x; // Create vector of complex numbers 
x.push_back(complex<float>(1, 2)); // Place 1 + j2 in vector 
x.push_back(complex<float>(2, 1)); // Place 2 + j1 in vector 

// Attempt at accessing the zero-th elements real part 
float temp1 = x.at(0).real; 
float temp2 = x[0].real; 

这给在Visual Studio 2015年以下错误:

严重性代码说明项目文件的线路抑制状态 错误C3867“的std :: _ Complex_base: :真实':非标准语法;使用 '&' 创建一个指向成员opencv_dft C:\用户\乔希\ VS_project \ main.cpp中101

+0

使用'x [0] .real()'或甚至更好'使用std :: real; ... real(x [0])'。 – alfC

回答

2

您在调用real()忘记括号。您需要:

float temp1 = x.at(0).real(); 
float temp2 = x[0].real(); 

real()是一个成员函数,而不是数据成员。

+0

哇,你是对的! – user8919

0

无需在声明x.push_back(complex(float){1,2})中进行投射 - 虽然不会伤害投射。另外不要忘记使用命名空间标准来使用矢量和复杂的语句。

另外不要忘记()s在x.at(0).real所以它是x.at(0).real();.

这是我如何使用向量和复数编写程序。

#include <iostream> 
#include <complex> 
#include <vector> 

    using namespace std; 

    void main() { 
     complex<float> a = { 1,2 }; 
     a = { 1,4 }; 
     vector<complex<float>> av; 
     av.push_back({ 1,2 }); 
     cout << av.at(0).real(); 
    } 
+0

非常好。非常感谢你。 – user8919