expected unqualified-id before ‘{’ token

hi all, just started a part time c++ course at cardiff uni and am stuck on my first excercise in "Class".
I have found 2 similar answers on the net but they use .....std:: in the answers, now as Ive not been shown that yet Im assuming that its not in the programme structure. Terminology is still elluding me.

Am running programme on Linux mint, petra.

Have simplified programme to the minimum for compile...and it refuses to... and any help would be much appreciated

the error is ...expected unqualified-id before ‘{’ token

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//excercise 1 tuesday 28 january 14 CUBE Problem...c++ E1.cpp -o E1
#include<iostream>
using namespace std;

class cube
{
private:
 double length;
public:
//cube constructor
 cube(double length_value)
 {
  
 	 length = length_value ;
 }
	 double base_area;

 {

 	 base_area = (length*length);
 }

	 
 	
 
};
 //Main programme
int main()
{
 	cube answer=cube(10);
	cout<<" Base area is ..."<<answer.base_area<<endl;
return 0;

}
Is line 18-21 supposed to be a function definition?
I dont know.

since posting I have been working with it and now error is....

E2.cpp:21:16: error: lvalue required as left operand of assignment
base_area() = length*length;

still searching the net for lvalue definition, and still terminology is foggy with me ....

dave



I think thats because you are trying to assign a value to a function not a variable.

base_area() is a function.
base_area is a variable.

i made some slight changes to the program and it works how i think you want it to.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//excercise 1 tuesday 28 january 14 CUBE Problem...c++ E1.cpp -o E1
#include<iostream>
using namespace std;

class cube
{
private:
 double length;

public:
//cube constructor
double base_area;
 cube(double length_value)
 {

 	 length = length_value ;
 	 base_area = (length*length);
 }




};
 //Main programme
int main()
{
 	cube answer=cube(10);
	cout<<" Base area is ..."<<answer.base_area<<endl;
return 0;

}


You were almost there.

The problem in the first piece of code you posted was this

1
2
3
4
 {

               base_area = (length*length);
 }

as it is not inside a function it causes a compile error.

All i did was move base_area = (length*length); into the constructor for the class cube(double length_value)
If this isnt what you wanted you could make a new function that does this.

Hope that helps
Thank you jidder (123) you are my hero. I have just tried it and it works perfectly. Brilliant

thanks, dave
Topic archived. No new replies allowed.