Program bug
Author |
Message |
Geminias
|
Posted: Thu Nov 03, 2005 5:55 pm Post subject: Program bug |
|
|
my compiler gives me an error at line 35 or so. i put a comment right beside where the error is.. i keep running into this error when i make classes and i have no idea what is wrong. any help would be appreciated.
syntax= c++
code: |
#include <iostream>
using namespace std;
enum Choice {
DrawRect = 1, GetArea, GetPerim, ChangeDimensions, Quit };
//rectangle class declaration
class Rectangle
{
public:
//constructors Rectangle (int width, int height);
~Rectangle();
// accessors
int GetHeight() const { return itsHeight; }
int GetWidth() const { return itsWidth; }
int GetArea() const {return itsHeight * itsWidth; }
int GetPerim() const { return 2*itsHeight + 2*itsWidth; }
void SetSize (int newWidth, int newHeight);
// Misc. methods
private:
int itsWidth;
int itsHeight;
};
//Class method implementations
void Rectangle::SetSize(int newWidth, int newHeight)
{
itsWidth = newWidth;
itsHeight = newHeight;
}
Rectangle::Rectangle (int width, int height) /* here is where
i get an error saying: prototype for 'Rectangle::Rectangle(int,int)'
does not match any in class 'Rectangle' */
{
itsWidth = width;
itsHeight = height;
}
Rectangle::~Rectangle() {}
int DoMenu();
void DoDrawRect (Rectangle);
void DoGetArea(rectangle);
void DoGetPerim (Rectangle);
/*==========================================*/
int main ()
{
//initialize a rectangle to 30,5
Rectangle theRect(30,5);
int choice = DrawRect;
int fQuit = false;
while (!fQuit)
{
choice = DoMenu();
if (choice < DrawRect || choice > Quit)
{
cout << "\nInvalid Choice, try again. " ;
cout << endl << endl;
continue;
}
switch (choice)
{
case DrawRect:
DoDrawRect(theRect);
break;
case GetArea:
DoGetArea (theRect);
break;
case GetPerim:
DoGetPerim(theRect);
break;
case ChangeDimensions:
int newLength, newWidth;
cout << "/nNew width: ";
cin >> newWidth;
cout << "New Height: ";
cin >> newLength;
theRect.SetSize(newWidth, newLenght);
DoDrawRect(theRect);
break;
caseQuit:
fQuit = true;
cout << "/nExiting... " << endl << endl;
break;
default:
cout << "Error in choice!" << endl;
fQuit = true;
break;
} //end switch
} // end while
return 0;
} // end main
int DoMenu()
{
int choice;
cout << endl << endl; // create two new lines
cout << " ***Menu*** " << endl;
cout << "(1) DrawRectangle" << endl;
cout << "(2) Area" << endl;
cout << "(3) Perimeter" << endl;
cout << "(4) Resize" << endl;
cout << "(5) Quit" << endl;
cint >> choice;
return choice;
}
void DoDrawRect (Rectangle theRect)
{
int height = theRect.GetHeight();
int width = theRect.GetWidth();
for (int i = 0; i < height ; i++)
{ cout << "*";
cout << endl;
}
}
void DoGetArea(Rectangle theRect)
{
cout << "Area: " << theRect.GetArea() << endl;
}
void DoGetPerim (Rectangle theRect)
{
cout << "Perimeter: " << theREct.GetPerim() << endl;
}
//====== End of listing =======
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Thu Nov 03, 2005 5:59 pm Post subject: (No subject) |
|
|
Perhaps because you've commented out the declaration of the constructor. You really need to work on code formatting, and numerous semantic issues as well. |
|
|
|
|
|
wtd
|
Posted: Thu Nov 03, 2005 6:01 pm Post subject: (No subject) |
|
|
Reducing the number of extraneous comments would reduce the potential for such problems. |
|
|
|
|
|
Geminias
|
Posted: Thu Nov 03, 2005 6:08 pm Post subject: (No subject) |
|
|
true that, thanks man |
|
|
|
|
|
wtd
|
Posted: Thu Nov 03, 2005 6:32 pm Post subject: (No subject) |
|
|
Cleaned up.
c++: | #include <iostream>
using namespace std;
enum Choice { DRAW_RECT = 1, GET_AREA, GET_PERIM, CHANGE_DIMENSIONS, QUIT };
class Rectangle
{
public:
Rectangle (int init_width, int init_height)
: width(init_width)
, height(init_height)
{
}
int get_height() const
{
return height;
}
int get_width() const
{
return width;
}
int get_area() const
{
return height * width;
}
int get_perim() const
{
return 2 * (height + width);
}
void set_size(int new_width, int new_height)
{
width = new_width;
height = new_height;
}
private:
int width, height;
};
int do_menu();
void do_draw_rectangle(Rectangle);
void do_get_area(Rectangle);
void do_get_perim(Rectangle);
/**************************************************/
int main ()
{
//initialize a rectangle to 30,5
Rectangle the_rect(30,5);
int choice = DRAW_RECT;
int f_quit = false;
while (!f_quit)
{
choice = do_menu();
if (choice < DRAW_RECT || choice > QUIT)
{
cout << endl << "Invalid Choice, try again. "
<< endl << endl;
continue;
}
switch (choice)
{
case DRAW_RECT:
do_draw_rectangle(the_rect);
break;
case GET_AREA:
do_get_area(the_rect);
break;
case GET_PERIM:
do_get_perim(the_rect);
break;
case CHANGE_DIMENSIONS:
int new_length, new_width;
cout << endl << "New width: ";
cin >> new_width;
cout << "New Height: ";
cin >> new_length;
the_rect.set_size(new_width, new_length);
do_draw_rectangle(the_rect);
break;
case QUIT:
f_quit = true;
cout << endl << "Exiting... " << endl << endl;
break;
default:
cout << "Error in choice!" << endl;
f_quit = true;
break;
}
}
return 0;
}
/**************************************************/
int do_menu()
{
int choice;
cout << endl << endl
<< " ***Menu*** " << endl
<< "(1) DrawRectangle" << endl
<< "(2) Area" << endl
<< "(3) Perimeter" << endl
<< "(4) Resize" << endl
<< "(5) Quit" << endl;
cin >> choice;
return choice;
}
void do_draw_rectangle(Rectangle the_rect)
{
int height = the_rect.get_height(),
width = the_rect.get_width();
for (int i = 0; i < height ; i++)
{
cout << "*" << endl;
}
}
void do_get_area(Rectangle the_rect)
{
cout << "Area: " << the_rect.get_area() << endl;
}
void do_get_perim(Rectangle the_rect)
{
cout << "Perimeter: " << the_rect.get_perim() << endl;
}
|
|
|
|
|
|
|
|
|