why does my complier complain there is an overloaded function?

Discussion in 'C++' started by Roy Yao, Aug 19, 2003.

  1. Roy Yao

    Roy Yao Guest

    Why the following code let my compiler complain an overloaded function
    Init()?

    // code begin
    template<class T> class BicircularList
    {
    template<class T> class Iterator;
    template<class T> class ChainNode
    {
    friend Iterator<T>;
    // ...
    };

    // ...
    public:
    template<class T> class Iterator
    {
    public:
    T* Init(const BicircularList<T>& list);
    T* Next();
    private:
    ChainNode<T> *m_pHead;
    ChainNode<T> *m_pCur;
    };

    // ...

    }

    template<class T>
    T* BicircularList<T>::Iterator<T>::Init(const BicircularList<T>& list)
    {
    m_pHead = const_cast<ChainNode<T> *> (&list.m_tNode);
    m_pCur = m_pHead->m_pNext;

    return (m_pCur!=m_pHead ? &m_pCur->m_tData : 0);
    }
    // end of code

    Here is the error message:
    error C2244: 'BicircularList<T>::Iterator<T>::Init' : unable to resolve
    function overload

    Who can help me? Thanks!

    Best regards.
    Roy
     
    Roy Yao, Aug 19, 2003
    #1
    1. Advertising

  2. Roy Yao

    Roy Yao Guest

    Thank Rob at first!

    But here is another question: why does the following code work correctly?

    // code begin
    template<class T> class SparseMatrix
    {
    template<class T> class ElemNode
    {
    friend SparseMatrix<T>;
    public:
    bool operator ==(const ElemNode<T>& nod) const;
    bool operator !=(const ElemNode<T>& nod) const;

    private:
    int m_nCol;
    T m_tVal;
    };

    template<class T> class HeadNode
    {
    friend SparseMatrix<T>;
    public:
    bool operator ==(const HeadNode<T>& nod) const;
    bool operator !=(const HeadNode<T>& nod) const;

    private:
    int m_nRow;
    LinkedList<ElemNode<T> > m_tCol;
    };

    public:
    SparseMatrix(int row = 0, int col = 0);
    SparseMatrix(const SparseMatrix<T>& sm);
    ~SparseMatrix();
    // ...

    private:
    const int cm_nRow;
    const int cm_nCol;
    LinkedList<HeadNode<T> > m_tRow;
    };

    template<class T>
    inline bool SparseMatrix<T>::ElemNode<T>::eek:perator ==(const ElemNode<T>&
    nod) const
    {
    return (m_tVal == nod.m_tVal);
    }

    template<class T>
    inline bool SparseMatrix<T>::ElemNode<T>::eek:perator !=(const ElemNode<T>&
    nod) const
    {
    return !(*this == nod);
    }
    // end of code
     
    Roy Yao, Aug 19, 2003
    #2
    1. Advertising

  3. Roy Yao wrote in news:bhs51j$1pnb$99.com:

    > Thank Rob at first!
    >
    > But here is another question: why does the following code work
    > correctly?


    AFAICT It doesen't, after commenting out LinkedList< .. etc (you
    supplied no declaration for this), I got:

    cl /W4 /Za /Zc:forScope,wchar_t /nologo /GR /Zi /TP /EHs test.cpp
    test.cpp
    test.cpp:49: error C2244: 'SparseMatrix<T>::ElemNode<T>::eek:perator`=='' :
    unable to match function definition to an existing declaration
    test.cpp:12: see declaration of
    'SparseMatrix<T>::ElemNode<T>::eek:perator`=='' definition
    'bool SparseMatrix<T>::ElemNode<T>::eek:perator ==(const
    SparseMatrix<T>::ElemNode<T> &) const' existing declarations
    'bool SparseMatrix<T>::ElemNode<T>::eek:perator ==(const
    SparseMatrix<T>::ElemNode<T> &) const'
    test.cpp:56: error C2244: 'SparseMatrix<T>::ElemNode<T>::eek:perator`!='' :
    unable to match function definition to an existing declaration
    test.cpp:13: see declaration of
    'SparseMatrix<T>::ElemNode<T>::eek:perator`!='' definition
    'bool SparseMatrix<T>::ElemNode<T>::eek:perator !=(const
    SparseMatrix<T>::ElemNode<T> &) const' existing declarations
    'bool SparseMatrix<T>::ElemNode<T>::eek:perator !=(const
    SparseMatrix<T>::ElemNode<T> &) const'


    cl (above) is VisualC 7.1 BTW.


    Maybe you have a non-conforming compiler. Note all the option's
    I had to give cl above to get it compile in a c++ standard
    conforming way.

    Any new code that you write should be tested with as many
    standard conforming (or as near as possible) compilers that
    you can get your hands on.

    >
    > // code begin
    > template<class T> class SparseMatrix
    > {
    > template<class T> class ElemNode


    Try changing this to:

    template<class X> class ElemNode

    > {


    You're missing the class again.

    > friend SparseMatrix<T>;


    friend class SparseMatrix<T>;

    Note that now we are in SparseMatrix<T>::ElemNode< X >
    and T is still available as a typename.

    > public:
    > bool operator ==(const ElemNode<T>& nod) const;
    > bool operator !=(const ElemNode<T>& nod) const;


    Replace with:

    bool operator ==(const ElemNode< X >& nod) const;
    bool operator !=(const ElemNode< X >& nod) const;
    Though:

    bool operator ==(const ElemNode & nod) const;
    bool operator !=(const ElemNode & nod) const;

    Should do.

    >
    > private:
    > int m_nCol;
    > T m_tVal;
    > };
    >


    Do the same as above, but maybe use Y instead of X, it doesn't
    matter but it will be less confusing.

    > template<class T> class HeadNode
    > {
    > friend SparseMatrix<T>;
    > public:
    > bool operator ==(const HeadNode<T>& nod) const;
    > bool operator !=(const HeadNode<T>& nod) const;
    >
    > private:
    > int m_nRow;


    //LinkedList<ElemNode<T> > m_tCol;

    > };
    >
    > public:
    > SparseMatrix(int row = 0, int col = 0);
    > SparseMatrix(const SparseMatrix<T>& sm);
    > ~SparseMatrix();
    > // ...
    >
    > private:
    > const int cm_nRow;
    > const int cm_nCol;


    //LinkedList<HeadNode<T> > m_tRow;

    > };
    >
    > template<class T>



    template < typename T > template < typename X >
    > inline bool SparseMatrix<T>::ElemNode< X >::eek:perator ==(const ElemNode
    > & nod) const
    > {
    > return (m_tVal == nod.m_tVal);
    > }
    >


    Note the double template above.

    [snip]

    I'm hopping the above will demonstrate member templates, well
    at least the syntax.

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Aug 19, 2003
    #3
  4. Roy Yao

    Roy Yao Guest

    Hello Rob

    Hello Rob,

    Sorry to distrub you again.

    But does your solution induce that class "Iterator" can not be instantiated
    when instantiating class "BicircularList"?

    Now I got this message(BTW: my compiler is VC++ 6):
    unresolved external symbol "public: int * __thiscall
    BicircularList<int>::Iterator::Next(void)"
    (?Next@Iterator@?$BicircularList@H@@QAEPAHXZ)

    Here is my unabridged class definition conforming your advise:
    // code begin
    template<class T> class BicircularList
    {
    public:
    class Iterator;

    private:
    class ChainNode
    {
    friend class BicircularList<T>; // BTW: Is the keyword class really
    necessary?
    friend class Iterator;
    private:
    T m_tData;
    ChainNode *m_pNext;
    ChainNode *m_pPrev;
    };

    public:
    class Iterator
    {
    public:
    T* Init(const BicircularList<T>& list);
    T* Next();
    private:
    ChainNode *m_pHead;
    ChainNode *m_pCur;
    };

    friend class Iterator;
    public:
    BicircularList();
    ~BicircularList();
    // ... methods omitted

    private:
    ChainNode m_tNode;
    };
    // end of code

    Best Regards
    Roy Yao


    "Rob Williscroft" <> wrote in message
    news:<Xns93DC25CD2F038ukcoREMOVEfreenetrtw@195.129.110.130>...
    > Roy Yao wrote in news:bhrve6$1lb7$99.com:
    >
    > > Why the following code let my compiler complain an overloaded function
    > > Init()?
    > >
    > > // code begin

    >
    >
    > template<class T> class BicircularList
    > {
    > // no template <class T>
    > class Iterator;
    > // no template <class T> again
    > class ChainNode
    > {
    > // class is required here
    > friend class Iterator;
    > // ...
    > };
    >
    > // ...
    > public:
    > // no template <class T>
    >
    > class Iterator
    > {
    > public:
    > T* Init(const BicircularList<T>& list);
    > T* Next();
    > private:
    > // removed a <T>
    > ChainNode *m_pHead;
    > ChainNode *m_pCur;
    > };
    >
    > // ...
    >
    > }; // don't forget this!
    >
    >
    > // removed a <T> after Iterator
    > template<class T>
    > T* BicircularList<T>::Iterator::Init(const BicircularList<T>& list)
    > {
    > m_pHead = const_cast<ChainNode<T> *> (&list.m_tNode);
    > m_pCur = m_pHead->m_pNext;
    >
    > return (m_pCur!=m_pHead ? &m_pCur->m_tData : 0);
    > }
    >
    > > Here is the error message:
    > > error C2244: 'BicircularList<T>::Iterator<T>::Init' : unable to resolve
    > > function overload
    > >

    >
    > HTH
    >
    > Rob.
    > --
    > http://www.victim-prime.dsl.pipex.com/
     
    Roy Yao, Aug 19, 2003
    #4
  5. Roy Yao

    Roy Yao Guest

    Re: Hello Rob

    Now I see. Thanks agin.

    "Rob Williscroft" <> wrote in message
    news:Xns93DC60478ABC1ukcoREMOVEfreenetrtw@195.129.110.201...
    > Roy Yao wrote in news:bhsh0r$22tf$99.com:
    >
    > > Now I got this message(BTW: my compiler is VC++ 6):

    >
    > This will at some point be a problem, VC++ 6.0 has very poor
    > template support.
    >
    > If upgrading is an option and you can suffer a command line
    > compiler then I'd suggest you download the .NET Framework SDK
    > it includes a version of 7.1. Its about 100M + download.
    >
    > Also there is a gcc windows port MinGW http://www.mingw.org/
    > a ~10M download.
    >
    > > unresolved external symbol "public: int * __thiscall
    > > BicircularList<int>::Iterator::Next(void)"
    > > (?Next@Iterator@?$BicircularList@H@@QAEPAHXZ)
    > >
    > >

    >
    > I didn't see a declaration of:
    >
    > template < class T >
    > T * BicircularList< T >::Iterator::Next()
    > {
    > // whatever ...
    > }
    >
    > Rob.
    > --
    > http://www.victim-prime.dsl.pipex.com/
     
    Roy Yao, Aug 21, 2003
    #5
    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. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,996
    Smokey Grindel
    Dec 2, 2006
  2. thinktwice
    Replies:
    3
    Views:
    338
    Daniel T.
    Feb 10, 2006
  3. Emre  Sevinc
    Replies:
    1
    Views:
    472
  4. Adam Akhtar
    Replies:
    9
    Views:
    170
    Seebs
    Jan 27, 2010
  5. Ronny
    Replies:
    3
    Views:
    189
    Ronny
    Feb 24, 2008
Loading...

Share This Page