Hi, I am new to C++ programming and have been studying it for about a week. I have run into a few problems, like all beginners, but have always been able to figure them out until now.The program I am stuck on is as follows:
//rectangle-02.cpp
#include <iostream>
class Rectangle {
public:
unsigned width, unsigned height;
void set_size(unsigned x, unsigned y);
unsigned area();
unsigned perimeter();
bool is_square();
}; //end of of class Rectangle
int main(){
unsigned width = 25;
unsigned height = 14;
std::cout << "With a width of " << width << " and a height of " << height << "...\n\n";
std::cout << "Press Enter to continue...";
std::cin.get();
return 0;
} //end of main
And the the primary error message (which I suspect to be the root cause for all the subsequent ones) is this:
rectangle-02.cpp:8: error: expected unqualified-id before ‘unsigned’
I've scowred the internet for this error but so far all I've learned is that you could get this error if you leave out a colon before specifying a class method or add an extraneous '()' after a class definition, neither of which are the problem here. I have gone over the code again and again and it seems to be identical to what is written in the book I am copying it from. The only conclusion I can come to is that there must be something wrong with my compiler -though it has worked just fine until now.
If it helps I am using the GNU C++ Compiler v 4.1 (the latest version) on a Debian Linux box.I would try another compiler if I could, but I can't find anything in the Debian libraries and I'm a little afraid to try anything that hasn't been approved.
You are missing quite a few braces. You need to close else statements when you open them, close function definitions, and define functions outside of other function bodies.
For example, you have something like:
1 2 3 4 5 6 7 8 9 10 11 12
void func1() {
//... code
void func2(); // You cannot define another function here, inside of another
return someVal; // You need to enclose this function body in braces anyway
if(/*...*/) {
//...
} else {
//You need to end the definition of func1()!
//main
EDIT: I rethought my response: You are missing a lot of braces. When you open any function, you need to close it. You also need to open functions when you are defining them.
e.g. the following is incorrect:
1 2 3 4
void Rectangle::set_size(unsigned x, unsigned y) {
width = x;
height = y;
// You need to end set_size() using a }
Thanks to both Zhuge and Grey Wolf, I rewrote it from scratch and got it to compile. Not only was I missing braces, I also had semicolons where they did not belong (like after the method statements). Learning C++ was easy until I got to the chapter on OOP which is an entirely new concept for me. Thank you again for your help.