1
#include <iostream>
using namespace std;

class C
{
    public:
    virtual string toString()
    {
        return "class C";
    }
};

class B : public C
{
    public:
    string toString()
    {
        return "class B";
    }
};

class A : public B
{
    public:
    string toString()
    {
        return "class A";
    }
};

void displayObject(C &p)
{
    cout << p.toString << endl;
}

int main()
{
    A a;
    B b;
    C c;    
    displayObject(a);
    displayObject(b);
    displayObject(c);
    return 0;
}

エラー

demo.cpp: In function ‘void displayObject(C&)’:
demo.cpp:33: errors:no match for ‘operator<<’ in ‘std::cout << p->C::toString’
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:108: 附注:备选为: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:117: 附注:         std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:127: 附注:         std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:165: 附注:         std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:169: 附注:         std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:173: 附注:         std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/ostream.tcc:91: 附注:         std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:180: 附注:         std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/ostream.tcc:105: 附注:         std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:191: 附注:         std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:200: 附注:         std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:204: 附注:         std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:209: 附注:         std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:213: 附注:         std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:221: 附注:         std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:225: 附注:         std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/ostream.tcc:119: 附注:         std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
4

2 に答える 2

5

出力する代わりに関数を呼び出してみてください:toString()

cout << p.toString() << endl;

括弧に注意してください:)

タイプのメンバー関数へのポインターを取るストリーム挿入演算子のオーバーロードがないため、印刷は機能しませんでしたstd::string(C::*)()

于 2013-03-15T01:14:15.847 に答える
1

印刷時に toString の後の () を忘れた

于 2013-03-15T01:14:55.823 に答える