Jun 9, 2011 at 1:50am UTC
I read the forums all the time and this is my first post, so please bare with me. I have a simple question on how to pass a vector to a member function in another class. This seems ignorant but I'm fulfilling an aspect of my assignment (class wrapper). It works within the same class, but when I move the printShapePtrVector to its own class I get the following error:
'ShapeVector' : base class undefined
Can someone please give me a hint of what I'm doing wrong - I thinks it's an easy fix if it's possible at all. Here are my two classes in question: Again, I'm new so if I'm posting it incorrectly, please let me know and I'll fix it.
The PrintVector class:
#include <iostream>
#include <vector>
using namespace std;
class PrintVector : public ShapeVector
{
public:
void displayShapePtrVector(vector<TwoDimensionalShape*> &intVectorRef)
{
vector<TwoDimensionalShape*>::const_iterator vecIterator;
for (vecIterator = intVectorRef.begin();vecIterator != intVectorRef.end();vecIterator++)
{
(*vecIterator)->draw();
(*vecIterator)->area();
Circle *myCirclePtr = dynamic_cast<Circle*>(*vecIterator);
if (myCirclePtr)
{
myCirclePtr->circumference();
cout << endl;
}
Triangle *myTrianglePtr = dynamic_cast<Triangle*>(*vecIterator);
if (myTrianglePtr)
{
myTrianglePtr->hypotenuse();
cout << endl;
}
Rectangle *myRectanglePtr = dynamic_cast<Rectangle*>(*vecIterator);
if (myRectanglePtr)
{
myRectanglePtr->perimeter();
cout << endl;
}
}
cout << endl;
}
private:
};
The calling function's class:
#include <iostream>
#include <vector>
#include "Circle.h"
#include "Triangle.h"
#include "Rectangle.h"
#include "TwoDimensionalShape.h"
using namespace std;
#include "PrintVector.h"
class Start
{
public:
Start()
{
}
virtual ~Start()
{
deleteShapePtrVector(shapePtrVector);
}
//void displayShapePtrVector(vector<TwoDimensionalShape*> &intVectorRef)
//{
// vector<TwoDimensionalShape*>::const_iterator vecIterator;
//
// for (vecIterator = intVectorRef.begin();vecIterator != intVectorRef.end();vecIterator++)
// {
// (*vecIterator)->draw();
// (*vecIterator)->area();
//
// Circle *myCirclePtr = dynamic_cast<Circle*>(*vecIterator);
// if (myCirclePtr)
// {
// myCirclePtr->circumference();
// cout << endl;
// }
// Triangle *myTrianglePtr = dynamic_cast<Triangle*>(*vecIterator);
// if (myTrianglePtr)
// {
// myTrianglePtr->hypotenuse();
// cout << endl;
// }
// Rectangle *myRectanglePtr = dynamic_cast<Rectangle*>(*vecIterator);
// if (myRectanglePtr)
// {
// myRectanglePtr->perimeter();
// cout << endl;
// }
// }
// cout << endl;
//}
void deleteShapePtrVector(vector<TwoDimensionalShape*> &intVectorRef)
{
vector<TwoDimensionalShape*>::const_iterator vecIterator;
TwoDimensionalShape *myShapePtr(0);
for (vecIterator = intVectorRef.begin();vecIterator != intVectorRef.end();vecIterator++)
{
myShapePtr = (*vecIterator);
delete myShapePtr;
myShapePtr = 0;
cout << "Shape is gone!" << endl;
}
}
int menu()
{
double radius = 0;
double base = 0;
double height = 0;
int userInput = -1;
do
{
cout << "\nWhat kind of object would you like to create?" << endl
<< "(1) Circle" << endl
<< "(2) Triangle" << endl
<< "(3) Rectangle" << endl
<< "(4) Print Shapes" << endl
<< "(5) Quit" << endl;
cin >> userInput;
if (userInput==1)
{
cout << "Input radius" << endl;
cin >> radius;
if (radius <=0)
cout << "Must be a positive number!" << endl;
else
{
TwoDimensionalShape *myShapePtr = new Circle(radius, radius);
shapePtrVector.push_back(myShapePtr);
}
}
else if (userInput==2)
{
cout << "Input base and height" << endl;
cin >> base
>> height;
if (base <=0 || height <=0)
cout << "Must be postive numbers!" << endl;
else
{
TwoDimensionalShape *myShapePtr = new Triangle(base, height);
shapePtrVector.push_back(myShapePtr);
}
}
else if (userInput==3)
{
cout << "Input base and height" << endl;
cin >> base
>> height;
if (base <=0 || height <=0)
cout << "Must be postive numbers!" << endl;
else
{
TwoDimensionalShape *myShapePtr = new Rectangle(base, height);
shapePtrVector.push_back(myShapePtr);
}
}
else if (userInput==4)
{
displayShapePtrVector(shapePtrVector); }//this is the problem line
else if (userInput==5)
break;
else
{
cout << "Invalid input" << endl;
system("cls");
}
}while(userInput);
return 0;
}
private:
vector<TwoDimensionalShape*> shapePtrVector;
};
int main()
{
system("color f4");
Start s;
s.menu();
system("pause");
return 0;
}
Sorry for the length, but I'm not sure how much to post. Thanks!
Jun 9, 2011 at 9:35am UTC
you should follow conventions and use good programming practices, Your code looks like a "speghetti". Idented code has more readability. In present condition, it is almost impossible to analize your code.
Jun 9, 2011 at 10:10am UTC
You can use the code format tags to format your code.
Have you looked at the code you posted? There isn't a definition for ShapeVector, but you've used it as a base class for PrintVector. That's what the compiler is complaining about.