1
#include <iostream>
using namespace std;

void input(partsType inventory[100]&)
{}

int main()
{
   struct partsType
   {

      string partName;
      int partNum;
      double price;
      int quantitiesInStock;
   };
   partsType inventory[100];
}

I am trying to use a struct variable as a formal parameter. Later I will pass the variable by reference.

Currently, I am getting the error

declaration is incompatible, and `partsType` is undefined. 
JeJo
  • 30,635
  • 6
  • 49
  • 88

1 Answers1

2

You have two issues:

  • You need to define the partsType outside the main, and of-course before the input function otherwise, it does not know what is partsType.
  • Secondly, your function parameter syntax is wrong. It should have been
    void input(partsType (&inventory)[100])
    //                   ^^^^^^^^^^^^^^^^^^  --> if you meant to pass the array by ref
    

So you need:

#include <iostream>
#include <string>   // missing header

struct partsType
{
   std::string partName;
   int partNum;
   double price;
   int quantitiesInStock;
};

void input(partsType (&inventory)[100]) 
{}

int main()
{
   partsType inventory[100];
}

Another option is to forward declare the struct partsType before the input function. But, that will need to make the function definition after the main, as you define the struct inside the main:

#include <iostream>
#include <string>   // missing header

// forward declaration
struct partsType;
void input(partsType(&inventory)[100]);

int main()
{
   struct partsType
   {
      std::string partName;
      int partNum;
      double price;
      int quantitiesInStock;
   };
   partsType inventory[100];
}
void input(partsType(&inventory)[100])
{
   // define
}

Also do not practice with using namespace std;

JeJo
  • 30,635
  • 6
  • 49
  • 88