What's wrong is that the main() you provided won't compile, so there's no way to diagnose your problem until you post the real one. I know you think it's compiling, but it's not valid C++.
Maybe it's not added to your VC++ project and you think it is? Export a makefile and post that, maybe.
You would do something like this:Originally Posted by jackfraust
Code:#include <iostream> // other includes etc // ... int main() { Shape* shape = getShape(); std::cout << shape->toString() << std::endl; delete shape; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
That means that you did not modify my example correctly.Originally Posted by jackfraust
Okay, I think we need to do some serious hand holding here. Ditch your code and try this:
Code:#include <iostream> class Shape { public: virtual ~Shape() {} virtual unsigned int area() const = 0; }; class Square : public Shape { public: explicit Square(unsigned int length) : length(length) {} virtual unsigned int area() const { return length * length; } private: unsigned int length; }; class Rectangle : public Shape { public: Rectangle(unsigned int length, unsigned int width) : length(length), width(width) {} virtual unsigned int area() const { return length * width; } private: unsigned int length; unsigned int width; }; Shape* getShape() { /* Okay, let's play a game: if the user enters an odd number, we create * a square of side 5. Otherwise, we create a 11 by 7 rectangle. */ std::cout << "Please enter an integer: "; int num; std::cin >> num; if (num % 2 == 0) { return new Rectangle(11, 7); } else { return new Square(5); } } int main() { Shape* shape = getShape(); std::cout << shape->area() << std::endl; delete shape; }
Last edited by laserlight; 04-09-2009 at 11:33 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
That's the thing, the getShapte() function is suppose to get the dimensions from the input stream, then store them in an array. The getShape function is supposed to be in getShape.cpp. Then the main function is suppose to be a different source file with only the Shape.h as #include header while the getShape is supposed to have the circle.h, rectangle.h and square.h as headers.
In that case, getShape() should return a std::vector<Shape*> instead, or if you are not allowed to use a standard container, then it should return a Shape**, i.e., you would create a dynamic array of Shape pointers. Each Shape pointer would then point to a derived class object.Originally Posted by jackfraust
That is not a problem, but I would expect a declaration of getShape() to be in a header. If you are not allowed to have a header for that, then just forward declare the function in the source file where the global main function resides.Originally Posted by jackfraust
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
It still doesn't work
here is what i wrote. I wrote again.
Code:#include "Circle.h" #include "Square.h" #include "Rectangle.h" using std::string; using namespace std; Shape* getShape() { do { string color; double length, width, rad, side; string shapetype; Shape *myshape; cout << "Enter the shape's color (or 'done')..." << endl; cin >> color; cout << "Enter shape type..." << endl; cin >> shapetype; if (shapetype == "done") { return NULL; } if (shapetype == "circle") { cout << "Enter the radius..." << endl; cin >> rad; myshape = new circle (color, rad); return myshape; } if (shapetype == "square") { cout << "Enter the side length..." << endl; cin >> side; myshape = new square (color, side); return myshape; } if (shapetype == "rectangle") { cout << "Enter the length..." << endl; cin >> length; cout << "Enter the width..." << endl; cin >> width; myshape = new rectangle (color, width, length); return myshape; } else{ cout << "Invalid input. Please enter \"circle\", \"square\", \"rectangle\" or \"done\"" << endl; continue; } }while (true); }
It still doesn't show anything when i run main.
Where's main?
this is the instruction for main:
The main.cpp file contains the main() function. It should define an array of base Shape pointers, and loops calling getShape() and storing the returned Shape pointer into the next available element of the array. The loop completes when a NULL Shape pointer is returned from getShape.
main() next sorts the array of Shape pointers into ascending order by area. Note that the areas of the shapes need to be compared, but the array to be sorted contains Shape pointers. When the sort is complete, main then loops and prints the sorted list of shapes.
Finally, main loops and calls delete on the Shape pointers to delete the shape objects.
main.cpp() #includes only the base Shape.h header file
Code:Shape *dshape[6]; int i; for ( i = 0; i < 6; ++i){ dshape[i]->getShape(); dshape[i]->toString(); }
Last edited by jackfraust; 04-11-2009 at 10:14 PM.
why is it that the compiler can't recognize the getShape() function.
Where does the compiler not recognise it?Originally Posted by jackfraust
Let's check: are you able to compile the source files for Circle, Square and Rectangle? Are you are to compile the source file that contains the definition (i.e., implementation) of getShape()?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
the source file shouldn't contain the implementation of getShape(). getshape is supposed to be the source file getShape.cpp and include the headers of circle.h, rectangle.h and square.h. Main.cpp is supposed to be a different source file and include only shape.h header.
I've already test the source codes i wrote for circle, rectangle and square. .The problem is with getShape() because i can't call it in main.
You are contradicting yourself. In the first sentence, you say that the source file should not contain the implementation of getShape(), then in the second second you say that the source file should contain the implementation of getShape().Originally Posted by jackfraust
Good. What about getShape.cpp? If it compiles, show your current Main.cpp. If not, show your current getShape.cpp.Originally Posted by jackfraust
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)