在 -std=c++11 上编译时的 C++ "no match for operator >>"

标签 c++ c++11 ifstream complextype

我有将数据(数字)从文件加载到复杂表的功能。在 -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;,同样的交易。你会必须读入临时变量,然后取决于我,您将写入 realimag

(写法示例: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/

相关文章:

c++ - 从其他类的成员函数返回 ENUM(在一个类中定义)

c++ - 将 3 个轴 vector 转换为轴/角度

c++ - 实现接口(interface)时对已删除函数的 unique_ptr 引用

c++ - 在头文件中使用模板

c++ - 在 C++ 中使用 ofstream 创建文本文件的问题

C++ 初始化基于类的全局变量

模板参数列表的 C++ 别名

c++ - 写入二进制文件时数据丢失-C++

c++ - 以列表格式读取文件时如何忽略换行符

c++ - 如何使用 googletest 失败进入断点