Search code examples
c++c++11ifstreamcomplextype

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


I have function that loads data (numbers) from file to complex table. Everything is compiling without error on -std=c++98, but when I want to compile with -std=c++11, probem with operator >> occurs.

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

Under that there is infromation about many canditates which cannot convert argument from double.

So what can I do, to run it with c++11 standard?


Solution

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

    None of these return reference, hence the value is not lvalue but rvalue(I believe), and you cannot assign to rvalues(Imagine writing dane >> 5;, the same deal. You will have to read into temporary variable and then depending on i you will either write to real or imag.

    (Example of writing: table[i /2].real(myTemporaryVariable);)

    Edit:

    Working function:

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

    }

    Also I have no idea why it compiles with -std=c++99