错误-函数定义C ++中的“ ...之前预期的主表达式”

问题描述

尝试在在线编译器中运行此错误,但出现错误-“预期的主表达式在'之前。”。在所有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;
        }