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

Discussion in 'C++' started by N4M, Aug 10, 2004.

  1. N4M

    N4M Guest

    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.
    N4M, Aug 10, 2004
    #1
    1. Advertising

  2. "N4M" <> schrieb im Newsbeitrag
    news:...
    > 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
    Gernot Frisch, Aug 10, 2004
    #2
    1. Advertising

  3. Gernot Frisch wrote:
    >
    > "N4M" <> schrieb im Newsbeitrag
    > news:...
    > > 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&
    >


    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
    Denis Remezov, Aug 10, 2004
    #3
  4. N4M

    N4M Guest

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

    Yeah, the point that MSVC6 does not support covariant return types!
    >
    > ________________________________________
    > Looking for a good game? Do it yourself!
    > GLBasic - you can do
    > www.GLBasic.com
    N4M, Aug 10, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -Steve-
    Replies:
    2
    Views:
    361
    - Steve -
    Jul 28, 2003
  2. John Harrison
    Replies:
    5
    Views:
    321
    - Steve -
    Jul 29, 2003
  3. Andy Jarrell

    Inheriting overloaded operators

    Andy Jarrell, Nov 13, 2003, in forum: C++
    Replies:
    5
    Views:
    429
    Victor Bazarov
    Nov 13, 2003
  4. Pete Wilson
    Replies:
    1
    Views:
    311
    Mike Wahler
    Apr 3, 2004
  5. NKOBAYE027
    Replies:
    2
    Views:
    346
    NKOBAYE027
    May 8, 2004
Loading...

Share This Page