C ++“不匹配运算符>>”在-std = c ++ 11上进行通信时

时间:2014-10-22 16:53:04

标签: c++ c++11 ifstream complextype

我有从文件到复杂表加载数据(数字)的函数。一切都在-std = c ++ 98上编译而没有错误,但是当我想用-std = c ++ 11进行编译时,带有运算符>>的probem发生。

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()

在此之下,有很多关于不能将争论从双重转换的候选人的信息。

那么我该怎么做才能用c​​ ++ 11标准运行它?

2 个答案:

答案 0 :(得分:1)

http://en.cppreference.com/w/cpp/numeric/complex/imag

这些都没有返回引用,因此值不是左值而是右值(我相信),并且你不能分配给rvalues(想象一下写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

编译

答案 1 :(得分:1)

使用C ++ 11,std :: complex的real()和imag()成员变为constexpr,这意味着const。因此,没有运营商&gt;&gt;对他们来说了有关规范,请参阅http://en.cppreference.com/w/cpp/numeric/complex/imag。我不知道这种结构在C ++ 11中是如何工作的。