【问题标题】:error - 'expected primary expression before...' in function definition (C++) [closed]错误 - 函数定义(C++)中的“预期的主表达式......”[关闭]
【发布时间】: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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多