|
-
April 5th, 2003, 11:02 PM
#1
Templates and classes
Could someone tell me why this code...
list.hh
Code:
#ifndef _LIST_HH
#define _LIST_HH
template <class T>
class list
{
private:
class node
{
T m_data;
node *m_prev;
node *m_next;
};
public:
list();
private:
node m_base;
node *m_head;
};
#endif // _LIST_HH
list.cc
Code:
#include "list.hh"
list::list()
: m_head(m_base)
{
}
gives this error...
list.cc:3: syntax error before `::' token
im using GNU C++ 3.2.2
theBUSH
The next line is not true.
The previous line is true.
There are only 10 kinds of people in the world, those who know binary, and those who dont.
There's no place like 127.0.0.1.
-
April 5th, 2003, 11:18 PM
#2
The list (a bad name to begin with) is a template class. Therefore all of your list member functions need the "template <class T>" placed before it.
Code:
template <class T> // <--This is missing
list<T>::list() : m_head(m_base) { } //<<--Look at this line also
Also, there is a std::list class already available in the C++ standard library that is a linked list class. There is no need (unless this is a class exercise) to reinvent the wheel.
This is why the name you chose, "list", will confuse others, thinking that it is the std::list class. It may also cause problems if you ever do introduce any functions that are in the "std" namespace. Either change the name of the class, or put it in a namespace, or do the wise thing and use what the standard library already provides -- std::list.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|