Okay, all this compiles fine, but I probably have some bugs.
Code:
template< typename T_ >
class Linked_List
{
public:
Linked_List<T_>()
{
start_ptr = NULL;
current = NULL;
}
T_ * data;
Linked_List<T_> * nxt;// Pointer to next node
void add_node(T_)
{
Linked_List<T_> *temp, *temp2; // Temporary pointers
// Reserve space for new node and fill it with data
temp = new Linked_List<T_>;
temp->data = T_;
temp->nxt = NULL;
// Set up link to this node
if (start_ptr == NULL)
{
start_ptr = temp;
current = start_ptr;
}
else
{
temp2 = start_ptr;
// We know this is not NULL - list not empty!
while (temp2->nxt != NULL)
{
temp2 = temp2->nxt;
// Move to next link in chain
}
temp2->nxt = temp;
}
}
void display_list()
{
Linked_List<T_> *temp;
temp = start_ptr;
cout << endl;
if (temp == NULL)
cout << "The list is empty!" << endl;
else
{
while (temp != NULL)
{
// Display details for what temp points to
if (temp == current)
cout << " <-- Current node";
cout << endl;
temp = temp->nxt;
}
cout << "End of list!" << endl;
}
}
void delete_start_node()
{
Linked_List<T_> *temp;
temp = start_ptr;
start_ptr = start_ptr->nxt;
delete temp;
}
void delete_end_node()
{
Linked_List<T_> *temp1, *temp2;
if (start_ptr == NULL)
cout << "The list is empty!" << endl;
else
{
temp1 = start_ptr;
if (temp1->nxt == NULL)
{
delete temp1;
start_ptr = NULL;
}
else
{
while (temp1->nxt != NULL)
{
temp2 = temp1;
temp1 = temp1->nxt;
}
delete temp1;
temp2->nxt = NULL;
}
}
}
void move_current_on ()
{
if (current->nxt == NULL)
cout << "You are at the end of the list." << endl;
else
current = current->nxt;
}
void move_current_back ()
{
if (current == start_ptr)
cout << "You are at the start of the list" << endl;
else
{
node *previous; // Declare the pointer
previous = start_ptr;
while (previous->nxt != current)
{
previous = previous->nxt;
}
current = previous;
}
}
Linked_List<T_> * start_ptr;
Linked_List<T_> *current; // Used to move along the list
};
It compiles fine like I said, I'm gonna try to add some stuff.
Looks like the first error to come to my attention is with the add function.
I need to pass it a T, if T has been declared as an std::string why can't I give it "hello" as a parameter?
Like this:
Code:
Linked_List<std::string> list;
list.add_node("Hello!");
This produces an error in the add function that looks like this
Code:
------ Build started: Project: Blackjack, Configuration: Debug Win32 ------
Compiling...
Card_Game.cpp
c:\documents and settings\jcoleman\desktop\blackjack\blackjack\linked_list.h(23) : error C2275: 'T_' : illegal use of this type as an expression
c:\documents and settings\jcoleman\desktop\blackjack\blackjack\card_game.cpp(11) : see declaration of 'T_'
c:\documents and settings\jcoleman\desktop\blackjack\blackjack\linked_list.h(19) : while compiling class template member function 'void Linked_List<T_>::add_node(T_)'
with
[
T_=std::string
]
c:\documents and settings\jcoleman\desktop\blackjack\blackjack\card_game.cpp(11) : see reference to class template instantiation 'Linked_List<T_>' being compiled
with
[
T_=std::string
]
Build log was saved at "file://c:\Documents and Settings\jcoleman\Desktop\Blackjack\Blackjack\Debug\BuildLog.htm"
Blackjack - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I'll paste the function and bold the lines causing the errors:
Code:
void add_node(T_)
{
Linked_List<T_> *temp, *temp2; // Temporary pointers
// Reserve space for new node and fill it with data
temp = new Linked_List<T_>;
temp->data = T_;
temp->nxt = NULL;
// Set up link to this node
if (start_ptr == NULL)
{
start_ptr = temp;
current = start_ptr;
}
else
{
temp2 = start_ptr;
// We know this is not NULL - list not empty!
while (temp2->nxt != NULL)
{
temp2 = temp2->nxt;
// Move to next link in chain
}
temp2->nxt = temp;
}
}