PDA

View Full Version : expected : or ; before { token error



hatsoff
November 20th, 2012, 02:58 PM
void enqueue(const string& name, const int age)
{
node* newnode = new node(); // at this place
newnode->set(name, age);
newnode->setnext(NULL);
rear->setnext(newnode);
rear = newnode;
size++;
}

dwhitney67
November 20th, 2012, 03:17 PM
void enqueue(const string& name, const int age)
{
node* newnode = new node(); // at this place
newnode->set(name, age);
newnode->setnext(NULL);
rear->setnext(newnode);
rear = newnode;
size++;
}

In C++, if a constructor does not take any parameters, you do not specify the braces; otherwise the declaration (albeit in error) appears as if you are attempting to call 'new' on a function.

Change your statement to be:


node* newnode = new node;


P.S. Always post code in CODE tags -- so as to preserve code formatting.

hatsoff
November 20th, 2012, 04:29 PM
it did not solve the problem still getting same error.

spjackson
November 20th, 2012, 04:36 PM
More context is needed. The error lies outside the fragment you have posted.

hatsoff
November 20th, 2012, 05:28 PM
after changes ,it compiled right away but crashing, when i add name

hatsoff
November 20th, 2012, 05:33 PM
i think there is problem with getline function please see this am i doing right?

do
{

cout << "Enter the name of classmate " << endl;
getline(cin, nameofclassmate);
cout << "Enter the age " << endl;
cin >> ageofclassmate;
queue1.enqueue(nameofclassmate, ageofclassmate);
cout << "do you want to add more : (y/n)";
cin >> input;
} while (input == 'y' || 'Y');

spjackson
November 20th, 2012, 05:56 PM
In the previous thread (http://ubuntuforums.org/showthread.php?t=2085853) dwhitney67 wrote


1. Acquiring raw input from the command line. Don't assume cin will flush input stream of newline characters (ie <Enter> key) that you input. Using getline() is a good choice.

2. Don't assume user will start adding nodes. Check the validity of your queue's pointers before attempting to access them.

You need to pay attention to both of these pieces of advice.

Hint: what is the value of rear when you first do this?


rear->setnext(newnode);


Note that this loop runs forever:


do {
}
while (input == 'y' || 'Y');


I'm sure that there are many more mistakes, but you need to do your own homework.