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

Inner class involving templates giving problem

P: n/a
Hi,

Please have a look at this code

when compiling it is giving error:
error C2440: '=' : cannot convert from 'ListNode *' to 'List<T>::ListNode *'
with
[
T=float
]
when I added List<T>:: before ListNode for

template<typename T>
List<T>::ListNode * List<T>::Head(void),

it gives a series of errors like undefined identifier T.....

Please tell me what is wrong here...

Thanks,
//////////////////////////////////////////////////////////////
#ifndef PLIST__H__
#define PLIST__H__

class ListNode;

/**
LinkedList class
*/
template<typename T>
class List
{

public:
void Init(void);
void Cleanup(void);
BOOL AddItem(T data);
ListNode * Head(void);

private:
/**
A node of the list
*/
class ListNode
{
ListNode(void){}
ListNode(const T& data, ListNode *next):m_data(data), m_next(next){}

public:
T m_data; ///<data
ListNode *m_next; ///<link to the next node
};

uint16 m_num_elems; ///<Number of elements in the list
ListNode *m_pstart; ///<Pointer to the first element of the list

public:
class Iterator
{
public:
Iterator(List<T> *plist);
void operator++(void);
T Content(void);

private:
List<T> *m_plist; ///<Pointer to the list
ListNode *m_plistnode; ///<Pointer to the listnode
};
};

//////////////////////////////////////////////////////////
//List
//////////////////////////////////////////////////////////

/**
Init List
*/
template<typename T>
void List<T>::Init(void)
{
m_num_elems = 0;
m_pstart = NULL;
}

/**
Add item to the front of the list
\param[in] data Data to be stored in the list
*/
template<typename T>
BOOL List<T>::AddItem(T data)
{
BOOL res = TRUE;
m_pstart = new ListNode(data, m_pstart);
if(!m_pstart)
{
LOG_FAILED;
ERXIT;
}
m_num_elems++;
xit:
LOG_IF_FAILED;
return res;
}

/**
Get the pointer to the first item in the list
*/
template<typename T>
ListNode * List<T>::Head(void)
{
return m_pstart;
}

/**
Cleanup routine
*/
template<typename T>
void List<T>::Cleanup(void)
{

}

////////////////////////////////////////////////////////////
//Iterator
///////////////////////////////////////////////////////////

/**
CTOR for iterator
\param[in] plist Pointer to list
*/
template<typename T>
List<T>::Iterator::Iterator(List<T> *plist)
:m_plist(plist)
{
m_plistnode = plist->Head();
}

/**
Increment operator
*/
template<typename T>
void List<T>::Iterator::operator++(void)
{
m_plistnode = m_plistnode->m_next;
}

/**
Get the content of the node iterator currently points
return content of the node iterator currently points
*/
template<typename T>
T List<T>::Iterator::Content(void)
{
return m_plistnode->m_data;
}

#endif//PLIST__H__
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
darkstorm wrote:
Please have a look at this code

when compiling it is giving error:
error C2440: '=' : cannot convert from 'ListNode *' to 'List<T>::ListNode *'
with
[
T=float
]
when I added List<T>:: before ListNode for

template<typename T>
List<T>::ListNode * List<T>::Head(void),

it gives a series of errors like undefined identifier T.....

Please tell me what is wrong here...
[...]


Post your _complete_ code again and this time put it all in one
translation unit and get rid of things like 'BOOL' or 'uint16' or define
proper typedefs for them.

The template definition is not enough to see what's wrong. We need to see
how you [attempt to] use your template.

V
Jul 23 '05 #2

P: n/a
Victor Bazarov <v.********@comAcast.net> wrote in message news:<Vk*******************@newsread1.mlpsca01.us. to.verio.net>...
darkstorm wrote:
Please have a look at this code

when compiling it is giving error:
error C2440: '=' : cannot convert from 'ListNode *' to 'List<T>::ListNode *'
with
[
T=float
]
when I added List<T>:: before ListNode for

template<typename T>
List<T>::ListNode * List<T>::Head(void),

it gives a series of errors like undefined identifier T.....

Please tell me what is wrong here...
[...]
Post your _complete_ code again and this time put it all in one
translation unit and get rid of things like 'BOOL' or 'uint16' or define
proper typedefs for them.


You can replace BOOL by int, It is just a Bolean flag. uint16 can be
replaced by unsigned int. LOG_FAILED and ERXIT can be removed. Please
do try and reply. I am stuck there.....

The template definition is not enough to see what's wrong. We need to see
how you [attempt to] use your template.

V

Jul 23 '05 #3

P: n/a
darkstorm wrote:
Victor Bazarov <v.********@comAcast.net> wrote in message news:<Vk*******************@newsread1.mlpsca01.us. to.verio.net>...
darkstorm wrote:
Please have a look at this code

when compiling it is giving error:
error C2440: '=' : cannot convert from 'ListNode *' to 'List<T>::ListNode *'
with
[
T=float
]
when I added List<T>:: before ListNode for

template<typename T>
List<T>::ListNode * List<T>::Head(void),

it gives a series of errors like undefined identifier T.....

Please tell me what is wrong here...
[...]


Post your _complete_ code again and this time put it all in one
translation unit and get rid of things like 'BOOL' or 'uint16' or define
proper typedefs for them.

You can replace BOOL by int, It is just a Bolean flag. uint16 can be
replaced by unsigned int. LOG_FAILED and ERXIT can be removed. Please
do try and reply. I am stuck there.....


Please do try *what*? The template definition is immaterial without
the code that causes it to be instantiated.

Please repost the _complete_ code that causes the error message to be
emitted.
The template definition is not enough to see what's wrong. We need to see ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^how you [attempt to] use your template.

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Did you read this particular part of my reply?

You didn't provide enough information. I am not going to waste time
guessing how your template is supposed to be used. Please understand
that I have no mind reading abilities, you need to post what is needed
to solve the problem.

V
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.