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
//excercise 1 tuesday 28 january 14 CUBE Problem...c++ E1.cpp -o E1
#include<iostream>
usingnamespace 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.