473,385 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

why does my complier complain there is an overloaded function?

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
4 3186
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

70
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this...
3
by: Prakash Bande | last post by:
Hi, I have bool operator == (xx* obj, const string st). I have declared it as friend of class xx. I am now able to do this: xx ox; string st; if (&ox == st) { } But, when I have a vector<xx*>...
44
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case...
5
by: Seven Kast USA | last post by:
Hi pelase explain me complier design of c and i need a small example complier design all phases in c programming . by KAST
3
by: thinktwice | last post by:
my project contains tens of files, it compiles well . now i need to add several class implementation files to the project which are similar with the classes already exist in the project, so i add...
2
by: B. Williams | last post by:
I have an assignment for school to Overload the operators << and >and I have written the code, but I have a problem with the insertion string function. I can't get it to recognize the second of...
9
by: rohits123 | last post by:
I have an overload delete operator as below ////////////////////////////////// void operator delete(void* mem,int head_type) { mmHead local_Head = CPRMemory::GetMemoryHead(head_type);...
2
by: desktop | last post by:
If a function should work with different types you normally overload it: void myfun(int a){ // do int stuff } void myfun(std::string str){ // do string stuff }
2
by: subramanian100in | last post by:
overloaded operator=() -------------------------------- overloaded assignment operator should be a non-static MEMBER function of a class. This ensures that the first operand is an lvalue. If...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.