【发布时间】:2020-12-15 11:00:33
【问题描述】:
尝试在在线编译器中运行它时出现错误 - 'expected primary expression before '.' ' 在所有 4 个结构成员的函数定义中:高度、宽度、长度、体积。在 Visual Studio 中,错误是 ''box' 非法使用这种类型作为表达式。可能是什么原因?
#include <iostream>
using namespace std;
struct box
{
float height;
float width;
float length;
float volume;
};
void display(box amazon);
int main()
{
box amazon
{
10, 10, 10, 10
};
display(amazon);
return 0;
}
void display(box amazon)
{
cout<<"Box height: "<<box.height;
cout<<"Box width: "<<box.width<<"Box length: "<<box.length<<"Box volume: "<<box.volume;
}
【问题讨论】:
-
box是类型的名称。您的变量称为amazon -
在显示函数中
box.应该是amazon.你使用的类型好像它是一个变量
标签: c++ structure function-definition