Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Seek for help..linked list..urgent!!!

Reply
Thread Tools

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

 
 
thong mfong
Guest
Posts: n/a
 
      05-12-2013
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;
}
}
 
Reply With Quote
 
 
 
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      05-12-2013
On Sun, 12 May 2013 02:11:38 -0700, thong mfong wrote:

> 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.

<snip>
> template <class Type1, class Type2>
> void c_system<Type1,Type2> :: insert(Type1 dd, Type1 mm, Type1 yy,
> Type2 person, Type2 title,Type2 description) {

<snip>
> if(first ==NULL) //if there is empty list
> {
> first = newNode;
> last = newNode;
> }
> else
> {

<snip>
} // Missing brace for the else clause
> }
>


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
python-noob - which container is appropriate for later exportinginto mySql + matplotlib ? someone Python 45 04-15-2013 12:28 PM
GCC warnings for comma operator W Karas C Programming 16 04-12-2013 08:03 AM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net 2 07-29-2008 09:50 AM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net Building Controls 0 07-29-2008 01:37 AM



Advertisments