Search code examples
c++structurefunction-definition

error - 'expected primary expression before...' in function definition (C++)


Trying to run this in an online compiler I get the error - 'expected primary expression before '.' ' in function definition on all 4 structure members: height, width, length, volume. In Visual studio the error is ' 'box' illegal use of this type as an expression. What could be the cause?

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

Solution

  • In the display function you have to change box, which is the type, with amazon, which is the object

           void display(box amazon)
        
            {
                cout<<"Box height: "<<amazon.height;
                cout<<"Box width: "<<amazon.width<<"Box length: "<<amazon.length<<"Box volume: "<<amazon.volume;
            }