| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Problem with this programming I'm making!
Okay, I've looked over my thing like a million times and I can't figure out whats wrong! (Yea I'm pretty new at programming...)
Here's the code I'm working with...any help would be greatly appreciated! Thanks! RECT.HPP //Begin Rect.hpp #include <iostream> class Point //holds x,y coordinates { //no constructor, use default public: void SetX(int x) {itsX = x;} void SetY(int y) {itsY = y;} int GetX()const {return itsX;} int GetY()const {return itsY;} private: int itsX; int itsY; }; //end of Point class declaration class Rectangle { public: Rectangle(int top, int left, int bottom, int right); ~Rectangle(){} int GetTop() const {return itsTop;} int GetLeft() const {return itsLeft;} int GetBottom() const {return itsBottom;} int GetRight() const {return itsRight;} Point GetUpperLeft() const {return itsUpperLeft;} Point GetLowerLeft() const {return itsLowerLeft;} Point GetUpperRight() const {return itsUpperRight;} Point GetLowerRight() const {return itsLowerRight;} void SetUpperLeft(Point Location); void SetLowerLeft(Point Location); void SetUpperRight(Point Location); void SetLowerRight(Point Location); void SetTop(int top); void SetLeft(int left); void SetBottom(int bottom); void SetRight(int right); int GetArea() const; private: Point itsUpperLeft; Point itsLowerLeft; Point itsUpperRight; Point itsLowerRight; int itsTop; int itsLeft; int itsBottom; int itsRight; }; //end Rect.hpp RECT.CPP //Begin rect.cpp #include "Rect.hpp" Rectangle::Rectangle(int top, int left, int bottom, int right) { itsTop = top; itsLeft = left; itsBottom = bottom; itsRight = right; itsUpperLeft.SetX(left); itsUpperLeft.SetY(top); itsUpperRight.SetX(right); itsUpperRight.SetY(top); itsLowerLeft.SetX(left); itsLowerLeft.SetY(bottom); itsLowerRight.SetX(right); itsLowerRight.SetY(bottom); } void Rectangle::SetUpperLeft(Point Location) { itsUpperLeft = Location; itsUpperRight.SetY(Location.GetY()); itsLowerLeft.SetX(Location.GetX()); itsTop = Location.GetY(); itsLeft = Location.GetX(); } void Rectangle::SetLowerLeft(Point Location) { itsLowerLeft = Location; itsLowerRight.SetY(Location.GetY()); itsUpperLeft.SetX(Location.GetX()); itsBottom = Location.GetY(); itsLeft = Location.GetX(); } void Rectangle::SetLowerRight(Point Location) { itsLowerRight = Location; itsLowerLeft.SetY(Location.GetY()); itsUpperRight.SetX(Location.GetX()); itsBottom = Location.GetY(); itsRight = Location.GetX(); } void Rectangle::SetUpperRight(Point Location) { itsUpperRight = Location; itsUpperLeft.SetY(Location.GetY()); itsLowerRight.SetX(Location.GetX()); itsTop = Location.GetY(); itsRight = Location.GetX(); } void Rectangle::SetTop(int top) { itsTop = top; itsUpperLeft.SetY(top); itsUpperRight.SetY(top); } void Rectangle::SetLeft(int left) { itsLeft = left; itsUpperLeft.SetX(left); itsLowerLeft.SetX(left); } void Rectangle::SetBottom(int bottom) { itsBottom = bottom; itsLowerLeft.SetY(bottom); itsLowerRight.SetY(bottom); } void Rectangle::SetRight(int right) { itsRight = right; itsUpperRight.SetX(right); itsLowerRight.SetX(right); } int Rectangle::GetArea() const { int Width = itsRight-itsLeft; int Height = itsTop - itsBottom; return(Width*Height); } //compute are of the rectangle by finding corners, //establishing width and height and then multiply int main() { //initialize a locatl Rectangle variable Rectangle MyRectangle(100, 20, 50, 80); int Area = MyRectangle.GetArea(); std::cout << "Area: " << Area << "\n"; std::cout << "Upper Left X Coordinate: "; std::cout << MyRectangle.GetUpperLeft().GetX(); system("pause"); return 0; } ERRORS THAT I GOT Compiler: Default compiler Building Makefile: "C:\Dev-Cpp\Makefile.win" Executing make... make.exe -f "C:\Dev-Cpp\Makefile.win" all g++.exe -c Rect.cpp -o Rect.o -I"C:/Dev-Cpp/include/c++" -I"C:/Dev-Cpp/include/c++/mingw32" -I"C:/Dev-Cpp/include/c++/backward" -I"C:/Dev-Cpp/include" Rect.cpp: In function `int main()': Rect.cpp:100: `Rectangle' undeclared (first use this function) Rect.cpp:100: (Each undeclared identifier is reported only once for each function it appears in.) Rect.cpp:100: parse error before `(' token Rect.cpp:102: `MyRectangle' undeclared (first use this function) make.exe: *** [Rect.o] Error 1 Execution terminated |
|
#2
|
|||
|
|||
|
Quote:
Hmm, I've tried your example and it workd for me, so you have these options: You have to havefollowing files: rect.hpp, rect.cpp, test.cpp test.cpp ___________________ #include <rect.h> int main(int argc, char *argv[]) { ... } _____________________ rect.hpp ___________________ #ifndef RECT_HPP #define RECT_HPP .. your stuff .. #endif ___________________ rect.cpp __________________ #include <rect.hpp> .. your stuff .. __________________ Now try woth these 3 files and it should work. If it doesn't, problem is in you compiler so change it Since I use Linux, I tried this with Intel & gcc compilers, and it works flawlessly. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Problem with this programming I'm making! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|