使用g ++编译此程序中的编译器错误

时间:2013-03-15 01:12:41

标签: c++

#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>]

2 个答案:

答案 0 :(得分:5)

尝试调用函数toString()而不是打印它:

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

请注意括号:)

打印它不起作用,因为没有流插入运算符重载,它采用类型为std::string(C::*)()

的指针成员函数

答案 1 :(得分:1)

打印时忘记了toString之后的()