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

meng_sun 2011-01-23 11:17:24

# 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;
}

cpp(21) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int......

...全文
131 7 打赏 收藏 转发到动态 举报

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

• 打赏
• 举报

[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
• 打赏
• 举报

• 打赏
• 举报

``````#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;
}
``````

• 打赏
• 举报

double --> float 有精度的损失～～

679

linuxpython 技术论坛（原bbs）

• 近7日
• 近30日
• 至今