gpt4 book ai didi

复杂类的运算符>>中的 C++ 不明确重载

转载 作者:搜寻专家 更新时间:2023-10-31 01:54:58 25 4
gpt4 key购买 nike

无论我做什么,我都会不断收到模棱两可的过载错误......这是我的代码

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>&);
};

运营商>>实现

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;
}

主要方法

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 重载在那里工作得很好,即使它几乎和这里一样....

最佳答案

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

那个 const 是个问题 - 你需要在这里改变 cmplx,它不能是 const。这就是编译器所提示的:cmplx.realconst int inside that function.

关于复杂类的运算符>>中的 C++ 不明确重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8959617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com