| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1273 人关注过本帖
标题:新人求助,Point &p=NULL;
只看楼主 加入收藏
渐渐鱼
Rank: 1
等 级:新手上路
帖 子:80
专家分:0
注 册:2018-5-11
结帖率:90%
收藏
已结贴  问题点数:10 回复次数:2 
新人求助,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
搜索更多相关主题的帖子: Point int set display cout 
2018-06-01 15:02
SMRen
Rank: 2
等 级:论坛游民
帖 子:9
专家分:42
注 册:2018-3-9
收藏
得分:10 
让我这个菜鸟来回答一下吧,Point &p=NULL;p=&c;p=&d;这三条语句是不合法的,引用必须在声明时绑定被引用对象而且不能更改绑定对象,所以这三条应该去掉第一个声明,把 p=&c 替换为 Point &p = c; 把 p=&d 替换为 Point &p1 = d;但是这两条语句中的p, p1在后面并没有用到,会报警告;此外, c->set(1,2,3);  c->display();  d->set(1,2,3,4);   d->display();  中的->是用于指针的,这里要用 . ,即c.set(1,2,3);
我还没学到 virtual 关键字,所以类中的函数我不太能看懂,希望刚才说的没有说错,对你有所帮助。
2018-06-02 08:43
快速回复:新人求助,Point &p=NULL;
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026389 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved