#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];
}
我正在尝试使用结构变量作为形式参数。稍后,我将通过引用传递变量。目前,我遇到了错误
declaration is incompatible, and `partsType` is undefined.
最佳答案
您有两个问题:
partsType
和类(class)之外定义main
否则,在input
函数之前,它不知道partsType
是什么。 是
void input(partsType (&inventory)[100])
// ^^^^^^^^^^^^^^^^^^ --> if you meant to pass the array by ref
因此,您需要:
#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];
}
另一种选择是在
partsType
函数之前预先声明struct input
。但是,这需要在main之后进行函数定义,因为您在main
中定义了struct:#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
}
也不要练习
using namespace std;
关于c++ - 使用结构变量作为形式参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63157992/