This class is working fine when structure Node was embedded in the class
but I want to work when the structure node is outside form class
I am a newbie at templates
//Correct Working class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
#include <iostream>
using namespace std;
template <class Object>
class Circular_List
{
private:
struct Node
{
Object info;
Node *next;
Node() : info(0), next(NULL)
{ }
};
Node *head, *tail;
int order;
public:
Circular_List()
{
order = -2; // to exclude head and tail
head = create_node();
tail = create_node();
head->next = tail;
tail->next = head;
}
Node* create_node()
{
order++;
return new Node;
}
void free_node(Node *p)
{
p->next = NULL;
order--;
delete p;
}
void destroy_list()
{
while(head ->next != tail)
{
Node* temp = head->next;
head->next = temp->next;
free_node(temp);
}
}
int size()
{ return order; }
~Circular_List()
{ destroy_list(); }
};
|
The only diff I make is structure is outside class
Now bunch of errors...
g++ -Wall -c "Circular_LinkedList_2.cpp
Circular_LinkedList_2.cpp:19: error: ISO C++ forbids declaration of `Node' with no type
Circular_LinkedList_2.cpp:19: error: expected `;' before '*' token
Circular_LinkedList_2.cpp:33: error: ISO C++ forbids declaration of `Node' with no type
Circular_LinkedList_2.cpp:33: error: expected `;' before '*' token
Circular_LinkedList_2.cpp:39: error: expected `;' before "void"
Circular_LinkedList_2.cpp:39: error: `Node' is not a type
Circular_LinkedList_2.cpp:40: error: ISO C++ forbids declaration of `p' with no type
Circular_LinkedList_2.cpp: In constructor `Circular_List<Object>::Circular_List()':
Circular_LinkedList_2.cpp:27: error: `head' was not declared in this scope
Circular_LinkedList_2.cpp:27: error: there are no arguments to `create_node' that depend on a template parameter, so a declaration of `create_node' must be available
Circular_LinkedList_2.cpp:27: error: (if you use `-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
Circular_LinkedList_2.cpp:28: error: `tail' was not declared in this scope
Circular_LinkedList_2.cpp:28: error: there are no arguments to `create_node' that depend on a template parameter, so a declaration of `create_node' must be available
Circular_LinkedList_2.cpp: In member function `void Circular_List<Object>::free_node(int*)':
Circular_LinkedList_2.cpp:41: error: request for member `next' in `p->', which is of non-class type `int'
Circular_LinkedList_2.cpp: In member function `void Circular_List<Object>::destroy_list()':
Circular_LinkedList_2.cpp:48: error: `head' was not declared in this scope
Circular_LinkedList_2.cpp:48: error: `tail' was not declared in this scope
Circular_LinkedList_2.cpp:50: error: missing template arguments before '*' token
Circular_LinkedList_2.cpp:50: error: `temp' was not declared in this scope
Compilation failed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#include <iostream>
using namespace std;
template <class Object>
struct Node
{
Object info;
Node *next;
Node() : info(0), next(NULL)
{ }
};
template <class Object>
class Circular_List
{
private:
Node *head, *tail;
int order;
public:
Circular_List()
{
order = -2; // to exclude head and tail
head = create_node();
tail = create_node();
head->next = tail;
tail->next = head;
}
Node* create_node()
{
order++;
return new Node;
}
void free_node(Node *p)
{
p->next = NULL;
order--;
delete p;
}
void destroy_list()
{
while(head ->next != tail)
{
Node* temp = head->next;
head->next = temp->next;
free_node(temp);
}
}
int size()
{ return order; }
~Circular_List()
{ destroy_list(); }
};
|