新人求助,Point &p=NULL;
程序代码:
#include<iostream> using namespace std; class Point { public: Point(){} Point(int X,int Y) { x=X; y=Y; } virtual void set(int X,int Y) { x=X; y=Y; } virtual void display() { cout<<"x="<<x<<endl; cout<<"y="<<y<<endl; } protected: int x; int y; }; class Circle:public Point { public: Circle(){} Circle(int X,int Y,int Radius):Point(X,Y) { x=X; y=Y; radius=Radius; } void set(int X,int Y,int Radius) { x=X; y=Y; radius=Radius; } void display() { cout<<"x="<<x<<endl; cout<<"y="<<y<<endl; cout<<"半径="<<radius<<endl; } protected: int radius; }; class Cylinder:public Circle { public: Cylinder(){} Cylinder(int X,int Y,int Radius, int Height):Circle(X,Y,Radius) { x=X; y=Y; radius=Radius; height=Height; } void set(int X,int Y,int Radius, int Height) { x=X; y=Y; radius=Radius; height=Height; } void display() { cout<<"x="<<x<<endl; cout<<"y="<<y<<endl; cout<<"半径="<<radius<<endl; cout<<"高="<<height<<endl; } private: int height; }; int main() { Point &p=NULL; Point t; t.set(1,2); t.display(); Circle c; Cylinder d; p=&c; c->set(1,2,3); c->display(); p=&d; d->set(1,2,3,4); d->display(); return 0; }
运行结果:
/tmp/815282660/main.cpp:84:12: error: non-const lvalue reference to type 'Point' cannot bind to a temporary of type 'long'
Point &p=NULL;
^ ~~~~
/tmp/815282660/main.cpp:94:6: error: member reference type 'Circle' is not a pointer; maybe you meant to use '.'?
c->set(1,2,3);
~^~
.
/tmp/815282660/main.cpp:95:6: error: member reference type 'Circle' is not a pointer; maybe you meant to use '.'?
c->display();
~^~
.
/tmp/815282660/main.cpp:98:6: error: member reference type 'Cylinder' is not a pointer; maybe you meant to use '.'?
d->set(1,2,3,4);
~^~
.
/tmp/815282660/main.cpp:99:6: error: member reference type 'Cylinder' is not a pointer; maybe you meant to use '.'?
d->display();
~^~
.
5 errors generated.
exit status 1