C ++运算符中的模糊重载>>对于复杂的课程

时间:2012-01-22 07:12:59

标签: c++ operator-overloading iostream istream

无论我做什么,我都会经常出现模糊的过载错误.... 这是我的代码

COMPLEX.cpp代码

template<class C> class complex
{  
    C real,imag;

    public:

    //constructors
    complex(C real=0,C imag=0)
    {
        complex::real=real;
        complex::imag=imag;
    }

    complex(const complex& cmplx)
    {
        real=cmplx.real;
        imag=cmplx.imag;
    }

    //functions available
    complex<C> operator+(const complex&);
    complex<C> operator+(double);
    void operator=(const complex&);

    //friend functions available
    template<class F> friend complex<F> operator+(double,const complex<F>&);
    template<class F> friend istream& operator>>(istream&,const complex<F>&);
    template<class F> friend ostream& operator<<(ostream&,const complex<F>&);
 };

OPERATOR&GT;&GT;实施

template<class F> istream& operator>>(istream& cin,const complex<F>& cmplx)
{
    cout<<"\nenter real part followed by imaginary part : ";

    cin>>cmplx.real>>cmplx.imag;

    return cin;
}

MAIN方法

int main(void)
{
    complex<int> C1;
    complex<int> C2;
    complex<int> C3;

    cin>>C1>>C2>>C3;

    cout<<C1<<C2<<C3;

    C1=C1+3;
    C2=2+C2;
    C3=C1+C2;

    cout<<C1<<C2<<C3;

    complex<int> C4(C3);

    cout<<C4;

    return 0;   
}

编译器错误

COMPLEX.cpp: In function ‘std::istream& operator>>(std::istream&, const complex<C>&) [with F = int, std::istream = std::basic_istream<char>]’:

COMPLEX.cpp:91:7:   instantiated from here

COMPLEX.cpp:75:2: error: ambiguous overload for ‘operator>>’ in ‘cin >> cmplx.complex<int>::real’

COMPLEX.cpp:75:2: note: candidates are:
/usr/include/c++/4.6/istream:122:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__istream_type& (*)(std::basic_istream<_CharT, _Traits>::__istream_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>] <near match>

/usr/include/c++/4.6/istream:122:7: note:   no known conversion for argument 1 from ‘const int’ to ‘std::basic_istream<char>::__istream_type& (*)(std::basic_istream<char>::__istream_type&) {aka std::basic_istream<char>& (*)(std::basic_istream<char>&)}’

/usr/include/c++/4.6/istream:126:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__ios_type& (*)(std::basic_istream<_CharT, _Traits>::__ios_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>, std::basic_istream<_CharT, _Traits>::__ios_type = std::basic_ios<char>] <near match>

/usr/include/c++/4.6/istream:126:7: note:   no known conversion for argument 1 from ‘const int’ to ‘std::basic_istream<char>::__ios_type& (*)(std::basic_istream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}’

/usr/include/c++/4.6/istream:133:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>] <near match>

/usr/include/c++/4.6/istream:133:7: note:   no known conversion for argument 1 from ‘const int’ to ‘std::ios_base& (*)(std::ios_base&)’

/usr/include/c++/4.6/istream:241:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>] <near match>

/usr/include/c++/4.6/istream:241:7: note:   no known conversion for argument 1 from ‘const int’ to ‘std::basic_istream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}’

在此之前我写了一个MATRIX.cpp和istream重载工作正常,即使它几乎与这里相同....

1 个答案:

答案 0 :(得分:3)

template<class F> istream& operator>>(istream& cin,const complex<F>& cmplx)
                                                   ^^^^^

那个const是个问题 - 你需要在这里改变cmplx,它不能是const。这就是编译器抱怨的内容:cmplx.real在该函数中是const int