| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 413 人关注过本帖
标题:编程中的错误问题
只看楼主 加入收藏
风使者
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-4-20
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
编程中的错误问题
#include<iostream.h>
class Shape
{
public:
    virtual float area() {return 0.0;}
    virtual float volume() {return 0.0;}
    virtual void ShapeName()=0;
};


class Point
{
public:
    Point(float=0,float=0);
    void set_point(float,float);
    float getX() {return x;}
    float getY() {return y;}
    virtual void ShapeName() {cout<<"Point:";}
    friend ostream&operator << (ostream &,Point &);
protected:
    float x;
    float y;
};


Point::Point(float a,float b)
{x=a;y=b;}


void Point::set_point(float a,float b)
{x=a;y=b;}


ostream&operator << (ostream &output,Point &p)
{cout<<"["<<p.x<<","<<p.y<<"]"<<endl;
return output;}




class Circle:public Point
{
public:
    Circle(float=0,float=0,float=0);
    void set_circle(float);
    float getradius() {return radius;}
    virtual float area();
    virtual void ShapeName() {cout<<"Circle:";}
    friend ostream&operator << (ostream &,Circle &);
protected:
    float radius;
};


Circle::Circle(float a,float b,float c):Point(a,b)
{radius=c;}

void Circle::set_circle(float c)
{radius=c;}

float Circle::area()
{return (3.14159*radius*radius);}


ostream&operator << (ostream &output,Circle &c)
{cout<<"["<<c.x<<","<<c.y<<"], r="<<c.radius<<endl;
return output;}






class Cylinder:public Circle
{
public:
    Cylinder(float=0,float=0,float=0,float=0);
    void set_cylinder(float);
    float getheight() {return height;}
    virtual float area();
    virtual float volume();
    virtual void ShapeName() {cout<<"Cylinder:";}
    friend ostream&operator << (ostream &,Cylinder &);
private:
    float height;
};


Cylinder::Cylinder(float a,float b,float c,float h):Circle(a,b,c)
{height=h;}

void Cylinder::set_cylinder(float h)
{height=h;}

float Cylinder::area()
{return (2*Circle::area()+2*3.14159*radius*height);}

float Cylinder::volume()
{return (Circle::area()*height);}

ostream&operator << (ostream &output,Cylinder &cy)
{cout<<"["<<cy.x<<","<<cy.y<<"], r="<<cy.radius<<", h="<<cy.height<<endl;
return output;}






int main()
{
Point pt(3.2,4.5);
pt.ShapeName();
cout<<pt<<endl;
Circle c(2.4,1.2,5.6);
c.ShapeName();
cout<<c<<endl;
Cylinder cy(3.5,6.4,5.2,10.5);
cy.ShapeName();
cout<<cy<<endl;

Shape *pt1;
pt1=&pt;
pt1->ShapeName();
cout<<"x="<<pt.getX()<<",y="<<pt.getY()<<"\narea="<<pt1->area()<<"\nvolume="<<pt1->volume()<<endl;
pt1=&c;
pt1->ShapeName();
cout<<"x="<<c.getX()<<",y="<<c.getY()<<"\narea="<<pt1->area()<<"\nvolume="<<pt1->volume()<<endl;
pt1=&cy;
pt1->ShapeName();
cout<<"x="<<cy.getX()<<",y="<<cy.getY()<<"\narea="<<pt1->area()<<"\nvolume="<<pt1->volume()<<endl;
return 0;
}


这段程序编译时出现以下错误:
D:\工具安装包\vc++\MSDev98\MyProjects\例6_4\例6_4.cpp(123) : error C2440: '=' : cannot convert from 'class Point *' to 'class Shape *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
D:\工具安装包\vc++\MSDev98\MyProjects\例6_4\例6_4.cpp(126) : error C2440: '=' : cannot convert from 'class Circle *' to 'class Shape *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
D:\工具安装包\vc++\MSDev98\MyProjects\例6_4\例6_4.cpp(129) : error C2440: '=' : cannot convert from 'class Cylinder *' to 'class Shape *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
执行 cl.exe 时出错.

请问该如何解决!谢谢
搜索更多相关主题的帖子: return 
2011-04-20 21:45
debroa723
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:23
帖 子:862
专家分:1954
注 册:2008-10-12
收藏
得分:10 
class Point:public Shape


pt1=(Shape*)&pt;//其它同此
2011-04-20 22:05
一点
Rank: 2
来 自:焦作
等 级:论坛游民
帖 子:6
专家分:37
注 册:2011-4-21
收藏
得分:10 
同上楼
类之间的转型,主要存在在父子之间
子类向父类赋值,不需要强制类型转换,虚函数将调用子类的虚函数
父类像子类之间,需要强制类型转换,虚函数将调用子类的虚函数
先写个简单的程序试试,不要太复杂的!!

如果您不甘平庸,行动就应与众不同!
2011-04-21 20:52
一点
Rank: 2
来 自:焦作
等 级:论坛游民
帖 子:6
专家分:37
注 册:2011-4-21
收藏
得分:0 
如:
#include <iostream>
#include <cstring>
using namespace std;

class Shape{
public:
    void getName(){cout<<"Shape    ";}
    virtual void  f(){cout<<"xu Shape  ";}

};

class Point:public Shape{
public:
    void getName(){cout<<"Point     ";}
    virtual void f(){cout<<"xu Point   ";}
};
void main()
{
    Shape *s=new Shape;
    Point *p=new Point;
    s=p;
    s->getName();
    s->f();
    cout<<endl;// 输出“Shape”


    p=(Point *)s;
    p->getName();
    p->f();// 输出“Point”
    getchar();
}

如果您不甘平庸,行动就应与众不同!
2011-04-21 20:53
快速回复:编程中的错误问题
数据加载中...
 
   



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

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