#include<iostream>
#include<string>
using namespace std;
//const int Asize = 20;
void function(CandyBar &, char *b = "Millennium Munch", double w = 2.85, int c = 350);
void show(const CandyBar &);
struct CandyBar
{
//char brand[Asize];
string brand;
double weight;
int calorie;
};
int main()
{
CandyBar candy;
cout << "Enter brand:" << endl;
//cin.getline(candy.brand,Asize);
getline(cin, candy.brand);
cout << "Enter weight:" << endl;
cin >> candy.weight;
cout << "Enter calorie" << endl;
cin >> candy.calorie;
show(candy);
function(candy);
show(candy);
return 0;
}
void show(const CandyBar &cb)
{
cout << cb.brand << endl
<< cb.weight << endl
<< cb.calorie << endl;
}
void function(CandyBar &CB, char *b, double w, int c)
{
CB.brand = b;
CB.weight = w;
CB.calorie = c;
}
1>------ 已启动生成: 项目: homework8_2, 配置: Debug Win32 ------
1>homework8_2.cpp
1>e:\gfx\visual studio\cpp_primer_plus\unit8\homework8_2\homework8_2\homework8_2.cpp(5): error C2065: “CandyBar”: 未声明的标识符
1>e:\gfx\visual studio\cpp_primer_plus\unit8\homework8_2\homework8_2\homework8_2.cpp(5): error C2059: 语法错误:“,”
1>e:\gfx\visual studio\cpp_primer_plus\unit8\homework8_2\homework8_2\homework8_2.cpp(6): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>e:\gfx\visual studio\cpp_primer_plus\unit8\homework8_2\homework8_2\homework8_2.cpp(6): error C2143: 语法错误: 缺少“,”(在“&”的前面)
1>e:\gfx\visual studio\cpp_primer_plus\unit8\homework8_2\homework8_2\homework8_2.cpp(24): error C2664: “void show(const int)”: 无法将参数 1 从“CandyBar”转换为“const int”
1>e:\gfx\visual studio\cpp_primer_plus\unit8\homework8_2\homework8_2\homework8_2.cpp(24): note: 没有可用于执行该转换的用户定义的转换运算符,或者无法调用该运算符
1>e:\gfx\visual studio\cpp_primer_plus\unit8\homework8_2\homework8_2\homework8_2.cpp(25): error C3861: “function”: 找不到标识符
1>e:\gfx\visual studio\cpp_primer_plus\unit8\homework8_2\homework8_2\homework8_2.cpp(26): error C2664: “void show(const int)”: 无法将参数 1 从“CandyBar”转换为“const int”
1>e:\gfx\visual studio\cpp_primer_plus\unit8\homework8_2\homework8_2\homework8_2.cpp(26): note: 没有可用于执行该转换的用户定义的转换运算符,或者无法调用该运算符
1>已完成生成项目“homework8_2.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
1 回答
已采纳
onemoo
TA贡献883条经验 获得超454个赞
第 5、6 行的函数声明中都用到了 CandyBar 类型,可是 struct CandyBar 的声明在它们后面!
把 struct CandyBar 声明提前就可以了。
- 1 回答
- 0 关注
- 1594 浏览
添加回答
举报
0/150
提交
取消