我是 C++ 的新手。
我写了一个非常简单的程序,如下所示
#include<iostream>
using namespace std;
class index
{
protected:
int count;
public:
index()
{
count=0;
}
index(int c)
{
count=c;
}
void display()
{
cout<<endl<<"count="<<count;
}
void operator ++()
{
count++;
}
};
class index1:public index{
public:
void operator --()
{
count--;
}
};
int main()
{
index1 i;
i++;
cout<<endl<<"i="<<i.display();
i++;
cout<<endl<<"i="<<i.display();
i--;
cout<<endl<<"i="<<i.display();
}
但是当我在 G++ 中编译这段代码时,我得到了这个:
In file included from /usr/include/c++/4.7/iostream:40:0,
from inheritance.cpp:1:
/usr/include/c++/4.7/ostream:480:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char)
/usr/include/c++/4.7/ostream:480:5: note: template argument deduction/substitution failed:
inheritance.cpp:40:30: note: cannot convert ‘i.index1::<anonymous>.index::display()’ (type ‘void’) to type ‘char’
编辑
我改成它解决了这个问题cout<<endl<<"i="<<i.display();
。cout<<endl<<"i="; i.display();
但现在我得到
inheritance.cpp:39:3: error: no ‘operator++(int)’ declared for postfix ‘++’ [-fpermissive]