Seek for help..linked list..urgent!!!


T

thong mfong

I 'm C++ programming beginner..I need someone help me ..
There is error "expected primary-expression before "template""..I 'm really don't know how to fix it..my compiler is Dev C++..pls giv me a favor..
Thanks for helping...


#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

fstream fp; // fstream provides an interface to read and write data from files as input/output streams.
ofstream ofp; // ofstream provides an interface to write data to files as output streams

template <class Type1, class Type2>
struct nodeType
{
Type1 month;
Type1 day;
Type1 year;
Type2 participant;
Type2 task_title;
Type1 status;
nodeType<Type1,Type2> *link;
};

template <class Type1, class Type2>
class c_system
{
public:
void initialiseList();
bool isEmpty();
c_system();
void insert(Type1 dd, Type1 mm, Type1 yy, Type2 person, Type2 title,Type2 description);
void addDetail();

nodeType<Type1,Type2> *first;
nodeType<Type1,Type2> *last;
};

template <class Type1, class Type2>
c_system<Type1,Type2> :: c_system()
{
first = NULL;
last = NULL;

}

template <class Type1, class Type2>
void c_system<Type1,Type2> :: insert(Type1 dd, Type1 mm, Type1 yy, Type2 person, Type2 title,Type2 description)
{
nodeType<Type1,Type2> *current;
nodeType<Type1,Type2> *trailCurrent;
nodeType<Type1,Type2> *newNode;
bool found;
newNode = new nodeType<Type1,Type2>;

assert(newNode != NULL); //terminate the program if cant allocate memory//
newNode ->day = dd; //store details into node
newNode ->month =mm;
newNode ->year =yy;
newNode ->participant = person;
newNode ->task_title = title;
newNode -> des=description;
newNode ->link = NULL;
newNode ->status = "pending";

if(first ==NULL) //if there is empty list
{
first = newNode;
last = newNode;

}
else
{
current = first;
found = false;

while(current !=NULL &&!found)
{
if(current->participant >= newNode->participant)
{
found = true;
}
else
{
trailCurrent = current;
current = current ->link;
}
}
if(current == first)
{
newNode->link = first;
first = newNode;

}
else
{
trailCurrent->link = newNode;
newNode ->link = current;
if(current == NULL)

last = newNode;

}
}

template <class Type1, class Type2>
void c_system<Type1,Type2> ::addDetail()
{
Type1 d,m,y;
Type2 people,name_title;

cout<<"Please enter the following details."<<endl;
cout<<"Date: (dd mm yy)";
cin>>d>>m>>y;
cout<<"Participant: ";
gets(people);
cout<<"Title: ";
gets(name_title)
cout<<"Description: ";
gets(des)

insert(d, m,y,people,name_title); //call function to insert data into node of the list
}

template <class Type1, class Type2>
void c_system<Type1,Type2> :: displayList()
{
nodeType<Type1,Type2> *current;
current = first;

while(current != NULL) //while there are nodes in the list
{
cout<<"---------------------------------------------------------"<<endl;
cout<<"Date: "<<current->day<<current->month<<current->year<<endl;
cout<<"Participant: "<<current->participant<<endl;
cout<<"Title: "<<current->task_title<<endl;
cout<<"Description: "<<current->des<<endl;
cout<<"Status: "<<current->status<<endl;
cout<<"---------------------------------------------------------"<<endl;
cout<<endl;

current = current->link;
}
}

int main()
{
c_system<string,long long> obj;
time_t d = time(0);
tm* current_date = localtime(&d);
cout<<"Time and Date: "<<ctime(&d)<<endl;


cout<<" ***My Calendar*** "<<endl;
while(begin!=0)
{
cout<<endl;
cout<<"Select an option which you want to proceed. "<<endl;
cout<<"1.Add the Task"<<endl;
cout<<"2.Delete the Task"<<endl;
cout<<"3.Display all Task"<<endl;
cout<<"0.To exit the program"<<endl;
cin>>begin;
switch(begin)
{
case 0: cout<<"Program ended"<<endl;
break;
case 1: obj.addTask();
break;
case 2: obj.deleteTask();
break;
case 2: obj.displayList();
break;
default: cout<<"Invalid input."<<endl;
}
}

system("pause");
return 0;
}
}
 
Ad

Advertisements

B

Bart van Ingen Schenau

I 'm C++ programming beginner..I need someone help me .. There is error
"expected primary-expression before "template""..I 'm really don't know
how to fix it..my compiler is Dev C++..pls giv me a favor.. Thanks for
helping...

You are missing a closing brace. This causes the compiler to see the
'template' keyword in a context where it does not expect it.
Unfortunately, today's compilers are not sophisticated enough to reliably
identify the root-cause of the problem.

template <class Type1, class Type2>
void c_system<Type1,Type2> :: insert(Type1 dd, Type1 mm, Type1 yy,
Type2 person, Type2 title,Type2 description) {
if(first ==NULL) //if there is empty list
{
first = newNode;
last = newNode;
}
else
{

After fixing this, there are quite a few new problems in your code, but I
will leave most of them for yourself to find and fix.
There are two issues that want to point out:

1. A properly implemented new operator will *never* return NULL. If it
can't allocate the required memory, it will throw a std::bad_alloc
exception. Simply ignoring this exception will have the same end-result
as the assert statement you used. The assert itself is completely useless
as it will never fire. But good of you to consider the possibility of
insufficient memory.

2. I saw you using `gets` to read some strings. This is a big no-no,
because gets is just about the most dangerous function there is and it
can't really be used safely. It is better you simply forget that it
exists in the first place.
To read a string that might contain spaces, use `std::getline` (or
std::istream::getline if you are forced to use char arrays for some
reason).

Bart v Ingen Schenau
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top