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

Discussion in 'C++' started by thong mfong, May 12, 2013.

  1. thong mfong

    thong mfong Guest

    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;
    }
    }
     
    thong mfong, May 12, 2013
    #1
    1. Advertising

  2. 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
     
    Bart van Ingen Schenau, May 12, 2013
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Ritchey
    Replies:
    7
    Views:
    511
    emerth
    Jul 10, 2003
  2. Chris Ritchey

    Generating a char* from a linked list of linked lists

    Chris Ritchey, Jul 9, 2003, in forum: C Programming
    Replies:
    7
    Views:
    501
    emerth
    Jul 10, 2003
  3. fool
    Replies:
    14
    Views:
    542
    Barry Schwarz
    Jul 3, 2006
  4. joshd
    Replies:
    12
    Views:
    701
    John Carson
    Oct 2, 2006
  5. Replies:
    3
    Views:
    152
    Andreas Perstinger
    May 14, 2013
Loading...

Share This Page