Overloaded operators ++(), - - ()problem

N

N4M

Dear,
I have problems with overloaded operators ++() and --(). MSVC++ 6.0
compiler gives errors, one is shown as below:
"
c:\data\c++\mygraphs\graph.h(182) : error C2555: 'CGraphNodeIter::++'
: overriding virtual function differs from 'CGraphNodeIterI::++' only
by return type or calling convention
c:\data\c++\mygraphs\graph.h(141) : see declaration of
'CGraphNodeIterI'
"

Codes are as below (I give a full code, sorry if it is somewhat long):

// Class:CGraphNodeIterI
class CGraphNodeIterI //abstract class to interface : COMPLETE &&
MINIMAL (S. Meyer, Item 18)
{
public:
typedef ::TGraphNode TNode;// int
public:
// Constructors, Deconstructors
CGraphNodeIterI() {}
CGraphNodeIterI(const CGraphNodeIterI& rNode) {}
//~CGraphNodeIterI();
virtual ~CGraphNodeIterI() {}
virtual CGraphNodeIterI& operator=(const CGraphNodeIterI& rNode) {}
// Operators
virtual TNode operator *() const = 0;
virtual CGraphNodeIterI& operator ++() = 0; //Prefix
virtual CGraphNodeIterI& operator --() = 0; //Prefix
virtual bool operator ==(const CGraphNodeIterI& rNode) = 0;
virtual bool operator !=(const CGraphNodeIterI& rNode) = 0;

};
//----------------------------------------------------------------------------
// Node iterator
class CGraphNodeIter: public CGraphNodeIterI //implementation
{
public:
typedef ::TGraphNode TNode;//int
private:
TNode m_nNode;
public:
// Constructors, Deconstructors
CGraphNodeIter(): m_nNode(TNode()) {}
explicit CGraphNodeIter(TNode rNode): m_nNode(rNode) {}
CGraphNodeIter(const CGraphNodeIter& rNode): m_nNode(rNode.m_nNode)
{}
~CGraphNodeIter();

CGraphNodeIter& operator=(const CGraphNodeIter& rNode) {
if (this != & rNode) m_nNode = rNode.m_nNode;
return *this;
}
// Operators
TNode operator *() const { return m_nNode; };
CGraphNodeIter& operator ++() { ++m_nNode; return *this; } //Prefix
CGraphNodeIter& operator --() { ASSURE(m_nNode >=
1);--m_nNode; return *this;} //Prefix
bool operator ==(const CGraphNodeIter& rNode) { return m_nNode ==
rNode.m_nNode;}
bool operator !=(const CGraphNodeIter& rNode) { return !(*this ==
rNode);}

};

Could you take a look and tell me where I make mistake?
Thanks a lot.
 
G

Gernot Frisch

N4M said:
Dear,
I have problems with overloaded operators ++() and --(). MSVC++ 6.0
compiler gives errors, one is shown as below:
"
c:\data\c++\mygraphs\graph.h(182) : error C2555: 'CGraphNodeIter::++'
: overriding virtual function differs from 'CGraphNodeIterI::++' only
by return type or calling convention
c:\data\c++\mygraphs\graph.h(141) : see declaration of
'CGraphNodeIterI'

virtual CGraphNodeIterI& operator ++() = 0; //Prefix
CGraphNodeIter& operator ++() { ++m_nNode; return *this; } //Prefix

See the difference of your return type:
CGraphNodeIterI&
CGraphNodeIter&


--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
 
D

Denis Remezov

Gernot said:
See the difference of your return type:
CGraphNodeIterI&
CGraphNodeIter&

I hope you didn't mean to say that was an invalid way to override a
virtual function. As the OP pointed out himself, the problem was
that MSVC++ 6.0 didn't support it.

To the OP: take a close look at your operators == and !=. You are hiding
them in the derived class, not overriding.

Denis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top