不匹配'operator<<'在'std :: cout<< ::运算++(INT)(0)”

时间:2013-06-15 12:26:07

标签: c++ operator-overloading

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <sstream>
#include <typeinfo>

#define debug(args...) // Just strip off all debug tokens
using namespace std;
// CUT begin
#define debug(args...) {dbg,args;cout<<endl;}
struct debugger{template<typename T> debugger& operator ,(const T& v){cout<<v<<" ";return *this;}}dbg;
template <typename T1,typename T2> inline ostream& operator<<(ostream& os,const pair<T1,T2>& p){return os<<"("<<p.first<<", "<<p.second<<")";}
template<typename T>inline ostream&operator<<(ostream& os,const vector<T>& v){string delim="[";for(unsigned int i=0;i < v.size();i++){os<<delim<<v[i];delim=", ";}return os<<"]";}
template<typename T>inline ostream&operator<<(ostream& os,const set<T>& v){string delim="[";for (typename set<T>::const_iterator ii=v.begin();ii!=v.end();++ii){os<<delim<<*ii;delim=", ";}return os<<"]";}
template<typename T1,typename T2>inline ostream&operator<<(ostream& os,const map<T1,T2>& v){string delim="[";for (typename map<T1,T2>::const_iterator ii=v.begin();ii!=v.end();++ii){os<<delim<<*ii;delim=", ";}return os<<"]";}
// CUT end


class Vehicle
{
public:
    int n;
    Vehicle(int n):n(n){cout<<"Ctor Vehicle "<<n<<endl;}
    Vehicle(Vehicle& v):n(v.n){cout<<"Copy Ctor Vehicle "<<n<<endl;}
    virtual ~Vehicle(){cout<<"Dtor Vehicle "<<n<<endl;}
    virtual ostream& dump(ostream& os){return os<<"Vehicle("<<n<<")";}
    string to_str(){stringstream s; dump(s); return s.str();}
    Vehicle& operator++(){n++;return *this;}
    Vehicle operator++(int x){Vehicle v(*this); operator++(); return v;}
};

class Car: public Vehicle
{
public:
    Car(int n): Vehicle(n){cout<<"Ctor Car "<<n<<endl;}
    virtual ~Car(){cout<<"Dtor Car "<<n<<endl;}
    virtual ostream& dump(ostream& os){return os<<"Car("<<n<<")";}
    Car operator++(int x){Car v(*this); operator++(); return v;}
    Car& operator++(){n++; return *this;}
    /* data */
};
ostream& operator<<(ostream& os,  Vehicle& v)
{
    return v.dump(os);
}

int main(int argc, char const *argv[])
{
    Car c(10);
    // cout<<c++<<endl;
    cout<<c++<<endl;
    return 0;
}

cout<<c++<<endl;导致以下错误:

C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:16:0: warning: "debug" redefined [enabled by default]
C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:13:0: note: this is the location of the previous definition
C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp: In function 'int main(int, const char**)':
C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:57:9: error: no match for 'operator<<' in 'std::cout << Car::operator++(int)(0)'
C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:57:9: note: candidates are:
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:110:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:110:7: note:   no known conversion for argument 1 from 'Car {aka Car}' to 'std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&) {aka std::basic_ostream<char>& (*)(std::basic_ostream<char>&)}'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:119:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ios_type& (*)(std::basic_ostream<_CharT, _Traits>::__ios_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>, std::basic_ostream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:119:7: note:   no known conversion for argument 1 from 'Car {aka Car}' to 'std::basic_ostream<char>::__ios_type& (*)(std::basic_ostream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:129:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:129:7: note:   no known conversion for argument 1 from 'Car {aka Car}' to 'std::ios_base& (*)(std::ios_base&)'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:167:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:167:7: note:   no known conversion for argument 1 from 'Car {aka Car}' to 'long int'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:171:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:171:7: note:   no known conversion for argument 1 from 'Car {aka Car}' to 'long unsigned int'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:175:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:175:7: note:   no known conversion for argument 1 from 'Car {aka Car}' to 'bool'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/bits/ostream.tcc:93:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/bits/ostream.tcc:93:5: note:   no known conversion for argument 1 from 'Car {aka Car}' to 'short int'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:182:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:182:7: note:   no known conversion for argument 1 from 'Car {aka Car}' to 'short unsigned int'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/bits/ostream.tcc:107:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/bits/ostream.tcc:107:5: note:   no known conversion for argument 1 from 'Car {aka Car}' to 'int'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:193:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:193:7: note:   no known conversion for argument 1 from 'Car {aka Car}' to 'unsigned int'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:202:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:202:7: note:   no known conversion for argument 1 from 'Car {aka Car}' to 'long long int'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:206:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:206:7: note:   no known conversion for argument 1 from 'Car {aka Car}' to 'long long unsigned int'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:211:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:211:7: note:   no known conversion for argument 1 from 'Car {aka Car}' to 'double'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:215:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:215:7: note:   no known conversion for argument 1 from 'Car {aka Car}' to 'float'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:223:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:223:7: note:   no known conversion for argument 1 from 'Car {aka Car}' to 'long double'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:227:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:227:7: note:   no known conversion for argument 1 from 'Car {aka Car}' to 'const void*'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/bits/ostream.tcc:121:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/bits/ostream.tcc:121:5: note:   no known conversion for argument 1 from 'Car {aka Car}' to 'std::basic_ostream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}'
C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:18:95: note: template<class T1, class T2> std::ostream& operator<<(std::ostream&, const std::pair<_T1, _T2>&)
C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:19:77: note: template<class T> std::ostream& operator<<(std::ostream&, const std::vector<T>&)
C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:20:74: note: template<class T> std::ostream& operator<<(std::ostream&, const std::set<T>&)
C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:21:91: note: template<class T1, class T2> std::ostream& operator<<(std::ostream&, const std::map<T1, T2>&)
C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:48:10: note: std::ostream& operator<<(std::ostream&, Vehicle&)
C:\Users\Rajat\Documents\GitHub\interview-preparation\cpp_test.cpp:48:10: note:   no known conversion for argument 2 from 'Car {aka Car}' to 'Vehicle&'
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:528:77: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*)
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:523:75: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*)
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:510:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*)
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/bits/ostream.tcc:323:70: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*)
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:493:72: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const _CharT*)
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:473:70: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char)
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:468:68: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char)
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:462:61: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char)
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:456:63: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char)
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/ostream:451:65: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT)
c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.6.1/include/c++/bits/basic_string.h:2694:59: note: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
[Finished in 1.2s with exit code 1]

为什么会出现这样的错误?

1 个答案:

答案 0 :(得分:8)

您的operator +会返回Car类型的临时格式,该格式不能绑定到左值引用(这是operator <<接受的),因为临时对象是 rvalues (更准确地说, prvalues )。

将插入操作符更改为:

ostream& operator<<(ostream& os, Vehicle const& v)
//                                       ^^^^^
{
    return v.dump(os);
}

因此,使dump()成为const成员函数。这一切都有意义,因为应该提供对象表示的函数不应该改变该对象的状态:

class Vehicle
{
    // ...
    virtual ostream& dump(ostream& os) const {return os<<"Vehicle("<<n<<")";}
//                                     ^^^^^
    // ...
};

class Car: public Vehicle
{
    // ...
    virtual ostream& dump(ostream& os) const {return os<<"Car("<<n<<")";}
//                                     ^^^^^
    // ...
};