c++ - 使用复 vector 时出错

标签 c++ vector complex-numbers

我需要在 C++ 中访问复杂数据 vector 的特定元素。

这是我的:

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 中产生以下错误:

Severity Code Description Project File Line Suppression State Error C3867 'std::_Complex_base::real': non - standard syntax; use '&' to create a pointer to member opencv_dft c : \users\josh\VS_project\main.cpp 101

最佳答案

您忘记了调用 real() 时的括号。你需要:

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

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

关于c++ - 使用复 vector 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45874751/

相关文章:

c++ - 如何绘制存储在 vector 中的 SFML 形状

c++ - 在特殊情况函数中展开 for 循环

C++ vector.erase() 函数错误

c++ - 依赖于默认模板类型参数的非类型模板参数

c++ - 将 Vector 传递给另一个类

c++ - 如何从两个 vector (实数和虚数)中获取复数 vector

python - Numpy 和 Matlab 在数组乘法方面的区别

C99 复杂支持与 visual studio

c++ - 快速的线程间通信机制

c++ - 为什么我不能使用嵌套在结构中的结构作为类型来在类模板中声明变量?