问题描述
尝试在在线编译器中运行此错误,但出现错误-“预期的主表达式在'之前。”。在所有4个结构成员的函数定义中:'高度,宽度,长度,体积。在Visual Studio中,错误是“框”非法使用此类型作为表达式。可能是什么原因?
#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
};
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;
}
解决方法
在display
函数中,您必须更改box
(即类型)和amazon
(即对象)
void display(box amazon)
{
cout<<"Box height: "<<amazon.height;
cout<<"Box width: "<<amazon.width<<"Box length: "<<amazon.length<<"Box volume: "<<amazon.volume;
}