D
darkstorm
Please check this program...When I compiles it in VC.net, it gives the
following error:
===============
Common\Lib\PList.h(115): error C2440: '=' : cannot convert from
'ListNode *' to 'List<T>::ListNode *'
with
[
T=float
]
Types pointed to are unrelated; conversion requires
reinterpret_cast, C-style cast or function-style cast
project\Source\Levelmgr.cpp(198) : while compiling
class-template member function 'List<T>::Iterator::Iterator(List<T>
*)'
with
[
T=float
]
project\Source\Levelmgr.cpp(15) : see reference to class
template instantiation 'List<T>::Iterator' being compiled
with
[
T=float
]
===============
when I tried to change it to
template<typename T>
List<T>::ListNode * List<T>::Head(void)
{
return m_pstart;
}
instead of
template<typename T>
ListNode * List<T>::Head(void)
{
return m_pstart;
}, it gives the error:
======================
Common\Lib\PList.h(89): error C2065: 'T' : undeclared identifier
======================
Could anyone please explain what is wrong here?
Thanks,
CODE:
______________________________________________________________________________
#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();//<------ERROR
}
/**
Increment operator
*/
template<typename T>
void List<T>::Iterator:
perator++(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__
following error:
===============
Common\Lib\PList.h(115): error C2440: '=' : cannot convert from
'ListNode *' to 'List<T>::ListNode *'
with
[
T=float
]
Types pointed to are unrelated; conversion requires
reinterpret_cast, C-style cast or function-style cast
project\Source\Levelmgr.cpp(198) : while compiling
class-template member function 'List<T>::Iterator::Iterator(List<T>
*)'
with
[
T=float
]
project\Source\Levelmgr.cpp(15) : see reference to class
template instantiation 'List<T>::Iterator' being compiled
with
[
T=float
]
===============
when I tried to change it to
template<typename T>
List<T>::ListNode * List<T>::Head(void)
{
return m_pstart;
}
instead of
template<typename T>
ListNode * List<T>::Head(void)
{
return m_pstart;
}, it gives the error:
======================
Common\Lib\PList.h(89): error C2065: 'T' : undeclared identifier
======================
Could anyone please explain what is wrong here?
Thanks,
CODE:
______________________________________________________________________________
#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();//<------ERROR
}
/**
Increment operator
*/
template<typename T>
void List<T>::Iterator:
{
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__