By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

why does my complier complain there is an overloaded function?

P: n/a
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



Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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>::operator ==(const ElemNode<T>&
nod) const
{
return (m_tVal == nod.m_tVal);
}

template<class T>
inline bool SparseMatrix<T>::ElemNode<T>::operator !=(const ElemNode<T>&
nod) const
{
return !(*this == nod);
}
// end of code


Jul 19 '05 #2

P: n/a
Roy Yao wrote in news:bh***********@mail.cn99.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>::operator`=='' :
unable to match function definition to an existing declaration
test.cpp:12: see declaration of
'SparseMatrix<T>::ElemNode<T>::operator`=='' definition
'bool SparseMatrix<T>::ElemNode<T>::operator ==(const
SparseMatrix<T>::ElemNode<T> &) const' existing declarations
'bool SparseMatrix<T>::ElemNode<T>::operator ==(const
SparseMatrix<T>::ElemNode<T> &) const'
test.cpp:56: error C2244: 'SparseMatrix<T>::ElemNode<T>::operator`!='' :
unable to match function definition to an existing declaration
test.cpp:13: see declaration of
'SparseMatrix<T>::ElemNode<T>::operator`!='' definition
'bool SparseMatrix<T>::ElemNode<T>::operator !=(const
SparseMatrix<T>::ElemNode<T> &) const' existing declarations
'bool SparseMatrix<T>::ElemNode<T>::operator !=(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 >::operator ==(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/
Jul 19 '05 #3

P: n/a
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" <rt*@freenet.REMOVE.co.uk> wrote in message
news:<Xn**********************************@195.129 .110.130>...
Roy Yao wrote in news:bh***********@mail.cn99.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/


Jul 19 '05 #4

P: n/a
Now I see. Thanks agin.

"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message
news:Xn**********************************@195.129. 110.201...
Roy Yao wrote in news:bh***********@mail.cn99.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/

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.