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

C++ "no match for operator >>" when comiling on -std=c++11

本文关键字:gt no match for operator C++ -std c++11 编译      更新时间:2023-10-16

我有一个函数,可以将文件中的数据(数字)加载到复杂的表中。在-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()

在这种情况下,有许多无法将参数从双精度转换为双精度的数据。

那么,我能做些什么,用c++11标准运行它呢?

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

这些都没有返回引用,因此该值不是左值,而是右值(我相信),并且你不能分配给右值(想象一下写dane >> 5;,同样的处理。你必须读入临时变量,然后根据I,你将写入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 编译

在C++11中,std::complex的real()和imag()成员变成了constexpr,这意味着const。因此,他们再也没有操作员>>了。看见http://en.cppreference.com/w/cpp/numeric/complex/imag用于规范。我看不出这个构造如何在C++11中工作。