why does my complier complain there is an overloaded function?

R

Roy Yao

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
 
R

Roy Yao

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
 
R

Rob Williscroft

Roy Yao wrote in
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 said:

You're missing the class again.
friend SparseMatrix<T>;

friend class SparseMatrix<T>;

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 said:
};

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

private:
const int cm_nRow;
const int cm_nCol;

//LinkedList said:
};

template<class T>


template said:
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.
 
R

Roy Yao

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
 
R

Roy Yao

Now I see. Thanks agin.

Rob Williscroft said:
Roy Yao wrote in

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.


I didn't see a declaration of:

template < class T >
T * BicircularList< T >::Iterator::Next()
{
// whatever ...
}

Rob.
 

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

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top