我有将数据(数字)从文件加载到复杂表的功能。在 -std=c++98 上编译一切都没有错误,但是当我想用 -std=c++11 编译时,会出现带有运算符 >> 的问题。
template <typename T> void load(char name[], complex<T> table[], int len) {
ifstream dane;
dane.open(name);
for (int i = 0; i < 2 * len; i++)
(i % 2 == 0 ? dane >> table[i / 2].real() : dane >> table[(i - 1) / 2].imag());
dane.close();
}
no match for 'operator>>' in 'dane >> (table + ((sizetype)(((unsigned int)((i + -1) / 2)) * 16u)))->std::complex<double>::imag()'
no match for 'operator>>' in 'dane >> (table + ((sizetype)(((unsigned int)(i / 2)) * 16u)))->std::complex<double>::real()
下面有关于许多候选人无法将参数从 double 转换的信息。
那么我该怎么做才能使用 c++11 标准运行它?
最佳答案
http://en.cppreference.com/w/cpp/numeric/complex/imag
这些都没有返回引用,因此值不是左值而是右值(我相信),并且你不能分配给右值(想象一下写 dane >> 5;
,同样的交易。你会必须读入临时变量,然后取决于我,您将写入 real
或 imag
。
(写法示例:table[i/2].real(myTemporaryVariable);
)
编辑:
工作函数:
template <typename T> void load(char name[], complex<T> table[], int len) {
ifstream dane;
dane.open(name);
for (int i = 0; i < 2 * len; i++)
{
double read;
dane >> read;
if (!(i%2)) table[i / 2].real(read);
else table[(i - 1) / 2].imag(read);
}
dane.close();
我也不知道为什么它用 -std=c++99 编译
关于在 -std=c++11 上编译时的 C++ "no match for operator >>",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26512849/