연산자 오버로드를 사용하는 클래스가 있지만 경고가 있습니다.스트림 연산자 오버로드 문제
// base.h
class base {
public:
base();
base(int n);
virtual ~base();
virtual void printBase(std::ofstream & out);
virtual base & operator =(const base &);
friend std::ofstream & operator <<(std::ofstream & out, const base &);
private:
double * coeff;
int n;
};
// base.cpp
std::ofstream & operator<<(std::ofstream & fout, const base & obj)
{
for(int i =0; i<(obj.n)-1; i++)
{
fout << obj.coeff[i]<<"*x"<<i;
if(i<obj.n-2)
{
fout<<"+";
}
}
fout<<"="<<obj.coeff[(obj.n)-1];
return fout;
}
void base::printBase(std::ofstream & fout)
{
for(int i =0; i<n-1; i++)
{
fout<<coeff[i]; // warning occurs here!!
if(i<n-2)
{
fout<<"+";
}
}
fout<<"="<<coeff[n-1];
}
경고는 다음과 같습니다
위의 경고에서>
warning: ISO C++ says that these are ambiguous, even though the worst conversion for
the first is better than the worst conversion for the second:
c:\wascana\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/ostream.tcc:105:5: note:
candidate 1: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
..\Hyperplane.cpp:55:17: note: candidate 2: std::ofstream& operator<<(std::ofstream&, const Hyperplane&)
, 그것은 <<
의 문제가 될 것이다. 이유는 알지만 어떻게이 경고를 처리 할 수 있습니까?
감사합니다.
아차 :
또 다른 해결책은 첫 번째 인수가 일치뿐만 아니라 내장 된 후보의 첫 번째 인수하도록하여 과부하를 변경하는 것 . 사과드립니다. 한 번에 세 명이 편집하는 것처럼 보입니다. [편집 : 5 또는 7 같은 것을 만들 ...] –