弱弱的求教:一个简单C++程序错误改正

meng_sun 2011-01-23 11:17:24
下面是写的第一个C++的简单练习题,在vs上敲了后有好多错误,求教解释。程序如下:


# include <iostream>
const float PI = 3.14;
const float FencePrice = 35;
const float ConcretePrice = 20;
class Circle
{
private:
float radius;
public:
Circle (float r);//construct function
float Circumference() const;//circumference of circle
float Area() const;//area of circle

};
//behavior of the class
Circle ::Circle(float r)
{radius = r;
}
//circumference of circle
Circle::Circumference() const
{return 2*radius*PI;
}
//area of circle
Circle::Area() const
{return PI*radius*radius;
}

void main()
{
float radius;
float FenceCost;
float ConcreteCost;
//input radius
cout <<"Enter the radius of the pool:";cin >> radius;
//state the object of the class Circle
Circle Pool (radius);
Circle PoolRim(radius+3);
//caculate the cost of the fence and output it
FenceCost = PoolRim.Circumference ()*FencePrice;
cout<<"FenceCost is RMB"<<FenceCost<<endl;
//caculate the cost of the concrete
Concrete Cost=(PoolRim.Area ()-Pool.Area ())*ConcretePrice;
cout<<"ConcreteCost is RMB "<<ConcreteCost<<endl;
}



错误其中有:warning C4305: “初始化”: 从“double”到“const float”截断
cpp(21) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int......

最好给修改后讲解下是怎么错的,百度了后部怎么理解。。。谢谢各位的帮助了哈。
...全文
131 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
meng_sun 2011-01-24
  • 打赏
  • 举报
回复


>g:\c++practice\test1_caculatethecost\test1_caculatethecost\caculatetheprice.cpp(3) : warning C4305: “初始化”: 从“double”到“const float”截断
1>g:\c++practice\test1_caculatethecost\test1_caculatethecost\caculatetheprice.cpp(22) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>g:\c++practice\test1_caculatethecost\test1_caculatethecost\caculatetheprice.cpp(22) : error C2556: “int Circle::Circumference(void) const”: 重载函数与“float Circle::Circumference(void) const”只是在返回类型上不同
1> g:\c++practice\test1_caculatethecost\test1_caculatethecost\caculatetheprice.cpp(12) : 参见“Circle::Circumference”的声明
1>g:\c++practice\test1_caculatethecost\test1_caculatethecost\caculatetheprice.cpp(22) : error C2371: “Circle::Circumference”: 重定义;不同的基类型
1> g:\c++practice\test1_caculatethecost\test1_caculatethecost\caculatetheprice.cpp(12) : 参见“Circle::Circumference”的声明
1>g:\c++practice\test1_caculatethecost\test1_caculatethecost\caculatetheprice.cpp(26) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>g:\c++practice\test1_caculatethecost\test1_caculatethecost\caculatetheprice.cpp(26) : error C2556: “int Circle::Area(void) const”: 重载函数与“float Circle::Area(void) const”只是在返回类型上不同
1> g:\c++practice\test1_caculatethecost\test1_caculatethecost\caculatetheprice.cpp(13) : 参见“Circle::Area”的声明
1>g:\c++practice\test1_caculatethecost\test1_caculatethecost\caculatetheprice.cpp(26) : error C2371: “Circle::Area”: 重定义;不同的基类型
1> g:\c++practice\test1_caculatethecost\test1_caculatethecost\caculatetheprice.cpp(13) : 参见“Circle::Area”的声明
1>g:\c++practice\test1_caculatethecost\test1_caculatethecost\caculatetheprice.cpp(40) : error C2264: “Circle::Circumference”: 函数定义或声明中有错误;未调用函数
1>g:\c++practice\test1_caculatethecost\test1_caculatethecost\caculatetheprice.cpp(43) : error C2065: “Concrete”: 未声明的标识符
1>g:\c++practice\test1_caculatethecost\test1_caculatethecost\caculatetheprice.cpp(43) : error C2146: 语法错误 : 缺少“;”(在标识符“Cost”的前面)
1>g:\c++practice\test1_caculatethecost\test1_caculatethecost\caculatetheprice.cpp(43) : error C2065: “Cost”: 未声明的标识符
1>g:\c++practice\test1_caculatethecost\test1_caculatethecost\caculatetheprice.cpp(43) : error C2264: “Circle::Area”: 函数定义或声明中有错误;未调用函数
1>g:\c++practice\test1_caculatethecost\test1_caculatethecost\caculatetheprice.cpp(43) : error C2264: “Circle::Area”: 函数定义或声明中有错误;未调用函数
1>生成日志保存在“file://g:\C++Practice\test1_CaculateTheCost\test1_CaculateTheCost\Debug\BuildLog.htm”
1>test1_CaculateTheCost - 12 个错误,1 个警告
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
meng_sun 2011-01-24
  • 打赏
  • 举报
回复
OK了。谢谢了!我基本功太薄弱了。。。
jeniss1234 2011-01-24
  • 打赏
  • 举报
回复
# include <iostream>
using namespace std;
const float PI = 3.14f;
const float FencePrice = 35;
const float ConcretePrice = 20;
class Circle
{
private:
float radius;
public:
Circle (float r);//construct function
float Circumference() const;//circumference of circle
float Area() const;//area of circle
};
//behavior of the class
Circle ::Circle(float r)
{
radius = r;
}
//circumference of circle
float Circle::Circumference() const
{
return 2*radius*PI;
}
//area of circle
float Circle::Area() const
{
return PI*radius*radius;
}

void main()
{
float radius;
float FenceCost;
float ConcreteCost;
//input radius
cout <<"Enter the radius of the pool:";
cin >> radius;
//state the object of the class Circle
Circle Pool (radius);
Circle PoolRim(radius+3);
//caculate the cost of the fence and output it
FenceCost = PoolRim.Circumference ()*FencePrice;
cout<<"FenceCost is RMB"<<FenceCost<<endl;
//caculate the cost of the concrete
ConcreteCost=(PoolRim.Area ()-Pool.Area ())*ConcretePrice;
cout<<"ConcreteCost is RMB "<<ConcreteCost<<endl;
}

const float PI = 3.14;
3.14编译器默认是double的,加上f告诉编译器这是一个float型

Circle::Circumference() const
改为
float Circle::Circumference() const
后面一样。
太乙 2011-01-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 meng_sun 的回复:]

还是有错误。。。
[/Quote]
admin@ubuntu:~/test$ g++ a.cpp 
admin@ubuntu:~/test$ ./a.out
Enter the radius of the pool:1
FenceCost is RMB879.2
ConcreteCost is RMB 942
admin@ubuntu:~/test$
meng_sun 2011-01-23
  • 打赏
  • 举报
回复
还是有错误。。。
太乙 2011-01-23
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
const float PI = 3.14;
const float FencePrice = 35;
const float ConcretePrice = 20;
class Circle
{
private:
float radius;
public:
Circle (float r);//construct function
float Circumference() const;//circumference of circle
float Area() const;//area of circle

};
//behavior of the class
Circle::Circle(float r)
{
radius = r;
}
//circumference of circle
float Circle::Circumference() const
{
return 2*radius*PI;
}
////area of circle
float Circle::Area() const
{
return PI*radius*radius;
}

int main()
{
float radius;
float FenceCost;
float ConcreteCost;
//input radius
cout <<"Enter the radius of the pool:";cin >> radius;
//state the object of the class Circle
Circle Pool (radius);
Circle PoolRim(radius+3);
//caculate the cost of the fence and output it
FenceCost = PoolRim.Circumference ()*FencePrice;
cout<<"FenceCost is RMB"<<FenceCost<<endl;
//caculate the cost of the concrete
ConcreteCost=(PoolRim.Area ()-Pool.Area ())*ConcretePrice;
cout<<"ConcreteCost is RMB "<<ConcreteCost<<endl;
return 0;
}
太乙 2011-01-23
  • 打赏
  • 举报
回复
double --> float 有精度的损失~~

679

社区成员

发帖
与我相关
我的任务
社区描述
智能路由器通常具有独立的操作系统,包括OpenWRT、eCos、VxWorks等,可以由用户自行安装各种应用,实现网络和设备的智能化管理。
linuxpython 技术论坛(原bbs)
社区管理员
  • 智能路由器社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧