痛苦死我了,程序编译出现 error link2001 ……
这个是程序:#include <iostream>using namespace std;
class shape
{public:
virtual float area() const {return 0.0;}
virtual void shapeName() const=0;
};
class Point:public shape
{public:
Point(float a=0.0,float b=0.0):x(a),y(b){}
void setPoint(float,float);
float getX() const {return x;}
float getY() const {return y;}
virtual void shapeName() const {cout<<"Point:";}
friend ostream & operator<<(ostream &,Point &);
protected:
float x;
float y;
};
//Point::Point(float a,float b)
//{x=a;y=b;}
void Point::setPoint(float a,float b)
{x=a;y=b;
}
ostream & operator<<(ostream &output,Point &p)
{output<<"["<<p.getX()<<","<<p.getY()<<"]";
return output;
}
class Circle:public Point
{public:
Circle(float x=0,float=0,float r=0):Point(x,y),radius(r){}
void setRadius(float);
float getRadius() const;
virtual void shapeName() const {cout<<"Circle:";}
//friend Circle operator+(Point &,Circle &);
friend Circle operator+(Circle &,Circle &);
friend ostream & operator<<(ostream &,Circle &);
protected:
float radius;
};
void Circle::setRadius(float r)
{radius=r;}
float Circle::getRadius() const
{return radius;}
Circle operator+(Circle &c1,Circle &c2)
{Circle c3;
c3.x=c1.getX()+c2.getX();
c3.y=c1.getY()+c2.getY();
c3.radius=c1.getRadius()+c2.getRadius();
return c3;
}
ostream & operator<<(ostream &output,const Circle &c)
{output<<"["<<c.getX()<<","<<c.getY()<<"],r="<<c.getRadius();
return output;
}
class Square:public Point
{public:
Square(float x=0,float y=0,float e=0):Point(x,y),edge(e){}
void setEdge(float);
float getEdge() const;
virtual void shapeName() const {cout<<"Square:";}
friend ostream & operator<<(ostream &,Square &);
protected:
float edge;
};
void Square::setEdge(float e)
{edge=e;}
float Square::getEdge() const
{return edge;}
ostream & operator<<(ostream &output,const Square &s)
{output<<"["<<s.getX()<<","<<s.getY()<<"],e="<<s.getEdge();
return output;
}
int main()
{Point p1(0.12f,0.13f);
p1.shapeName();
cout<<p1<<endl;
Circle c_1(12.1f,15.3f,13.5f),c_2(20.4f,456.4f,21.32f),c_3;
c_3=c_1+c_2;
cout<<c_3<<endl;
return 0;
}
程序非常简单 只用看Circle类就行 主函数主要是为了验证程序写的对不对,所以随便写的, 就是重载+之后出现了这个问题