无法编译我的程序,因为它会给我“未定义的引用”错误。
ide中没有语法错误,所以我不知道是什么引起了问题。
#include <iostream>
#include <string>
#include <cmath>
#include <vector>
using namespace std;
//classes
class coordinates;
class ShapeTwoD;
class Rectangle;
//declare functions
void choice1();
//vectors
vector<coordinates>coordinatesVec;
vector<ShapeTwoD*>ShapeTwoDVec;
class coordinates
{
private:
int x, y;
public:
coordinates(int x, int y)
{
this->x=1;
this->y=1;
}
void setX(int y)
{
this->x=x;
}
void setY(int y)
{
this->y=y;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
};
class ShapeTwoD
{
protected:
string name;
bool containsWarpSpace;
public:
//default constructor
ShapeTwoD()
{
this->name = "";
this->containsWarpSpace = false;
};
//other constructor
ShapeTwoD(string name, bool containsWarpSpace)
{
this->name = name;
this->containsWarpSpace = containsWarpSpace;
}
//copy constructor
ShapeTwoD(const ShapeTwoD& s)
{
this->name = s.name;
this->containsWarpSpace = s.containsWarpSpace;
}
//getname method
string getName()
{
return name;
}
//getContainsWarpSpace method
bool getContainsWarpSpace()
{
return containsWarpSpace;
}
//setName
void setName(string name)
{
this->name = name;
}
//setContainsWarpSpace
void setContainsWarpSpace(bool containsWarpSpace)
{
this->containsWarpSpace = containsWarpSpace;
}
//Virtual methods
virtual void setCoordinates(vector<coordinates>c) = 0;
virtual double computeArea();
virtual bool isPointInShape(int x, int y);
virtual bool isPointOnShape(int x, int y);
virtual string toString() = 0;
};//end of ShapeTwoD
class Rectangle: public ShapeTwoD
{
private:
vector<coordinates>vec;
public:
//constructor
Rectangle()
{
}
//set vector , the vector contains coordinates
void setCoordinates(vector<coordinates>v)
{
this->vec=v;
}
//to find length of rectangle side based on 2 given (x,y)
double distance(double x1, double x2, double y1, double y2)
{
return sqrt(pow(x2-x1,2)+pow(y2-y1,2));
}
//find area of rectangle
double computeArea()
{
double length = distance(vec[1].getX(), vec[2].getX(), vec[1].getY(), vec[2].getY());
double width = distance(vec[1].getX(), vec[0].getX(), vec[1].getY(), vec[0].getY());
return length * width;
}
bool isPointInShape(int x, int y)
{
return true;
}
bool isPointOnShape(int x, int y)
{
return true;
}
string toString()
{
string x = "hello";
return x;
}
};//end of Rectangle
int main()
{
bool quit;
while(quit == !true)
{
int option; //choice input
cout << endl;
cout << "Student ID : 6348063" << endl;
cout << "Student Name : Sirikorn Chatphatthananan" << endl;
cout << "------------------------------------------" << endl;
cout << "Welcome to Assn2 program!" << endl;
cout << endl;
cout << "1) Input sensor data" << endl;
cout << "2) Compute area (for all records)" << endl;
cout << "3) Print shapes report" << endl;
cout << "4) Sort shape data" << endl;
cout << "Please enter your choice: ";
cin >> option;
//enter choice "5" to quit the menu/program
if(option == 5)
{
cout << endl;
cout << "Quiting program....." << endl;
cout << endl;
quit = true;
}
if (option == 1)
{
choice1();
}
if (option == 2)
{
//at index 0 is the Rectangle object/pointer
ShapeTwoDVec[0]->computeArea();
}
}
//system("pause");
return 0;
}//end of main()
void choice1()
{
string shapename;
string specialType;
bool validShape = false;
bool validType = false;
cout << "[Input sensor data]" << endl;
while (validShape != true)
{
cout << "Please enter name of shape: ";
cin >> shapename;
if(shapename == "Rectangle" || shapename == "rectangle" )
{
//Create new ShapeTwoD object in heap
ShapeTwoD* rectangleP = new Rectangle;
int pointX, pointY;
//Ask for special or normal type
while(validType != true)
{
cout << "Please enter special type: ";
cin >> specialType;
if(specialType == "WS" || specialType == "ws")
{
rectangleP->setContainsWarpSpace(true);
validType = true;
}
else if(specialType == "NS" || specialType == "ns")
{
rectangleP->setContainsWarpSpace(false);
validType = true;
}
else
{
cout << "Wrong input" << endl;
}
}
try
{
//Register x and y coordinates for rectangle
for(int i=1; i<=4; i++)
{
cout << "Please enter x-ordinate of pt." << i <<": ";
cin >> pointX;
cout << "Please enter y-ordinate of pt." << i <<": ";
cin >> pointY;
//Each looping will add new coordinates objects to vector
coordinates recCoordinates(pointX, pointY);
coordinatesVec.push_back(recCoordinates);
}
}
catch(const invalid_argument &err)
{
cerr << "All coordinates must be an int type";
}
//Add items into Rectangle class in heap
rectangleP->setName(shapename);
rectangleP->setCoordinates(coordinatesVec);
//To test for isPointOnShape
//rectangleP->isPointOnShape(pointX, pointY);
//rectangleP->isPointInShape(pointX, pointY);
//Add all pointers into vector of ShapeTwoD
ShapeTwoDVec.push_back(rectangleP);
}
}
}//end of function for choice1
期望用户根据形状输入一些坐标。选项1要求输入坐标,然后程序会将其保存到向量中。选项2将使用这些坐标来计算不同形状的面积。