注册 登录
编程论坛 C++教室

教材上的例子,在C++6.0下总是编译不通过

marenshi 发布于 2010-06-13 23:59, 1460 次点击
请教在C++6.0环境编程的注意问题。
8 回复
#2
audioMan862010-06-14 12:45
你不列出错误信息,如何帮你了
#3
南国利剑2010-06-14 14:49
请楼主把详细情况说明一下。
#4
华仔li2010-06-14 23:22
你用的VC++是不是中文版的?
#5
marenshi2010-06-15 20:48
是啊,是中文版的
#6
marenshi2010-06-15 20:56
#include<iostream.h>
class Shape
{public:
virtual float area() const {return 0.0;}
virtual float volume() const {return 0.0;}
virtual void shapename() const =0;
};
class Point:public Shape
{public:
Point(float=0,float=0);
void setpoint(float =0,float =0);
float getX() const{return x;}
float getY() const{return y;}
virtual void shapename() const {cout<<"Point:";}
friend ostream &operator<<(ostream&, const 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,const Point p)
{output<<"["<<p.getX()<<","<<p.getY()<<"]";
return output;
}
class Circle:public Point
{public:
Circle(float x=0,float y=0,float r=0 );
void setradius(float);
float getradius()const;
virtual float area() const;
virtual void shapename() const {cout<<"Circle:";}
friend ostream &operator<<(ostream &,const Circle &);
protected:
    float radius;
};
Circle::Circle(float a,float b,float r):Point(a,b),radius(r){}
void Circle::setradius(float r)
{radius=r;}
float Circle::getradius() const {return radius;}
float Circle::area() const
{return 3.14159*radius*radius;}
ostream &operator<<(ostream &output,Circle &c)
{output<<"["<<c.getX()<<","<<c.getY()<<"],r="<<c.getradius();
return output;
}
class Cylinder:public Circle
{public:
Cylinder(float x=0,float y=0,float r=0,float h=0);
void setheight(float);
virtual float area()const;
virtual float volume() const;
virtual void  shapename() const{cout<<"Cylinder:";}
friend ostream &operator<<(ostream &,Cylinder &);
protected:
    float heigth;
};
Cylinder::Cylinder(float a,float b,float r,float h):Circle(a,b,r){height=h;}
void Cylinder::setheight(float h)
{height=h;}
float Cylinder::area() const
{return 2*Circle::area()+2*3.14159*radius*height;}
float Cylinder::volume()
{return Circle::area()*height;
}
ostream &operator<<(ostream &output,Cylinder &cy)
{output<<"["<<cy.getX()<<","<<cy.getY()<<"],r="<<cy.getradius()<<",h="<<cy.getheight();
return output;
}
int main()
{Point point(3.2,4.5);
Circle circle(2.4,1.2,5.6);
Cylinder(3.5,6.4,5.2,10.5);
point.shapename();
cout<<point<<endl;
circle.shapename();
cout<<circle<<endl;
cylinder.shapename();
cout<<cylinder<<endl;
Shape *p;
p->shapename();
cout<<"x="<<point.getX()<<"y="<<point.getY()<<"\narea="<<p->area()<<"\nvolume="<<p->volume()<<"\n\n";
p=&circle;
p->shapename();
cout<<"x="<<circle.getX()<<"y="<<circle.getY()<<"\nnarea="<<p->area()<<"\nvolume="<<p->volume()<<"\n\n";
p=&cylinder;
cout<<"x="<<cylinder.getX()<<"y="<<cylinder.getY()<<"\narea"<<p->area()<<"\nvolume="<<p->volume()<<"\n\n";
return 0;
}
C++6.0中文版,编译通不过。
:\我的文档\桌面\xx.cpp(44) : warning C4244: 'return' : conversion from 'double' to 'float', possible loss of data
d:\我的文档\桌面\xx.cpp(60) : error C2065: 'height' : undeclared identifier
d:\我的文档\桌面\xx.cpp(60) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
d:\我的文档\桌面\xx.cpp(62) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
d:\我的文档\桌面\xx.cpp(64) : warning C4244: 'return' : conversion from 'double' to 'float', possible loss of data
d:\我的文档\桌面\xx.cpp(66) : error C2511: 'volume' : overloaded member function 'float (void)' not found in 'Cylinder'
        d:\我的文档\桌面\xx.cpp(49) : see declaration of 'Cylinder'
d:\我的文档\桌面\xx.cpp(69) : error C2039: 'getheight' : is not a member of 'Cylinder'
        d:\我的文档\桌面\xx.cpp(49) : see declaration of 'Cylinder'
d:\我的文档\桌面\xx.cpp(73) : warning C4305: 'argument' : truncation from 'const double' to 'float'
d:\我的文档\桌面\xx.cpp(74) : warning C4305: 'argument' : truncation from 'const double' to 'float'
d:\我的文档\桌面\xx.cpp(74) : warning C4305: 'argument' : truncation from 'const double' to 'float'
d:\我的文档\桌面\xx.cpp(74) : warning C4305: 'argument' : truncation from 'const double' to 'float'
d:\我的文档\桌面\xx.cpp(75) : warning C4305: 'argument' : truncation from 'const double' to 'float'
d:\我的文档\桌面\xx.cpp(75) : warning C4305: 'argument' : truncation from 'const double' to 'float'
d:\我的文档\桌面\xx.cpp(80) : error C2065: 'cylinder' : undeclared identifier
d:\我的文档\桌面\xx.cpp(80) : error C2228: left of '.shapename' must have class/struct/union type
d:\我的文档\桌面\xx.cpp(88) : error C2440: '=' : cannot convert from 'int *' to 'class Shape *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
d:\我的文档\桌面\xx.cpp(89) : error C2228: left of '.getX' must have class/struct/union type
d:\我的文档\桌面\xx.cpp(89) : error C2228: left of '.getY' must have class/struct/union type
执行 cl.exe 时出错.

xx.obj - 1 error(s), 0 warning(s)
高手指教。谢谢。我都对C++开始灰心丧气了。
#7
cnfarer2010-06-15 22:40
#include<iostream>
//#include<stdlib.h>
using namespace std;
class Shape
{public:
virtual float area() const {return 0.0;}
virtual float volume() const {return 0.0;}
virtual void shapename() const =0;
};
class Point:public Shape
{public:
Point(float=0,float=0);
void setpoint(float =0,float =0);
float getX() const{return x;}
float getY() const{return y;}
virtual void shapename() const {cout<<"Point:";}
friend ostream &operator<<(ostream&, const 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,const Point p)
{output<<"["<<p.getX()<<","<<p.getY()<<"]";
return output;
}
class Circle:public Point
{public:
Circle(float x=0,float y=0,float r=0 );
void setradius(float);
float getradius()const;
virtual float area() const;
virtual void shapename() const {cout<<"Circle:";}
friend ostream &operator<<(ostream &,const Circle &);
protected:
    float radius;
};
Circle::Circle(float a,float b,float r):Point(a,b),radius(r){}
void Circle::setradius(float r)
{radius=r;}
float Circle::getradius() const {return radius;}
float Circle::area() const
{return 3.14159*radius*radius;}
ostream &operator<<(ostream &output,Circle &c)
{output<<"["<<c.getX()<<","<<c.getY()<<"],r="<<c.getradius();
return output;
}
class Cylinder:public Circle
{public:
Cylinder(float x=0,float y=0,float r=0,float h=0);
void setheight(float);
virtual float area()const;
virtual float volume() const;
virtual void  shapename() const{cout<<"Cylinder:";}
virtual float getheight() const{return height;}
friend ostream &operator<<(ostream &,Cylinder &);
protected:
    float height;
};
Cylinder::Cylinder(float a,float b,float r,float h):Circle(a,b,r){height=h;}
void Cylinder::setheight(float h)
{height=h;}
float Cylinder::area() const
{return 2*Circle::area()+2*3.14159*radius*height;}
float Cylinder::volume() const
{return Circle::area()*height;
}
ostream &operator<<(ostream &output,Cylinder &cy)
{output<<"["<<cy.getX()<<","<<cy.getY()<<"],r="<<cy.getradius()<<",h="<<cy.getheight();
return output;
}
int main()
{Point point(3.2,4.5);
Circle circle(2.4,1.2,5.6);
Cylinder cylinder(3.5,6.4,5.2,10.5);
point.shapename();
cout<<point<<endl;
circle.shapename();
cout<<circle<<endl;
cylinder.shapename();
cout<<cylinder<<endl<<endl;
Shape *p;
p=&point;
p->shapename();
cout<<"x="<<point.getX()<<"y="<<point.getY()<<"\narea="<<p->area()<<"\nvolume="<<p->volume()<<"\n\n";
p=&circle;
p->shapename();
cout<<"x="<<circle.getX()<<"y="<<circle.getY()<<"\narea="<<p->area()<<"\nvolume="<<p->volume()<<"\n\n";
p=&cylinder;
cout<<"x="<<cylinder.getX()<<"y="<<cylinder.getY()<<"\narea="<<p->area()<<"\nvolume="<<p->volume()<<"\n\n";

return 0;
}
#8
zgxyz20082010-06-20 00:11
一处height变量打错了,还有个getheight函数没有定义,缺少一个Cylinder对象名....
#9
qq2511682332010-06-20 17:10
小弟借此领教!
1