我有一個類使用操作符重載,但有一些警告。流操作符重載問題
// 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 ...] – 2011-01-28 01:32:19