c ++引用参数函数 [英] c++ reference parameters functions
问题描述
Im参考参数有问题。 getStockInfo
中的值应存储在参考参数中。我不知道如何做,使 displayStatus
接受那些作为参数。每当我把东西放到 getStockInfo
中时,它给我的错误重载函数getStockInfo多于一个onstance匹配参数列表
。
Im having trouble with the reference parameters. The values in getStockInfo
are supposed to be stored in reference parameters. I dont know how to do that so that displayStatus
accepts those as arguments. Whenever i put something into getStockInfo
in main it gives me the error More than one onstance of overloaded function "getStockInfo" matches the argument list
.
#include <iostream>
#include <iomanip>
using namespace std;
void getStockInfo(int &, int&, double&);
void displayStatus(int &, double &);
int main()
{
int spoolsOrdered;
int spoolsStock;
double specialCharges;
cout << "Middletown Wholesale Copper Wire Company" << endl;
getStockInfo(spoolsOrdered, spoolsStock, specialCharges);
}
void getStockInfo(int &spoolsOrdered, int &spoolsStock, double specialCharges)
{
char ship;
cout << "How many spools would you like to order: ";
cin >> spoolsOrdered;
//Validate the spools ordered
while(spoolsOrdered < 1)
{
cout << "Spools ordered must be at least one" << endl;
cin >> spoolsOrdered;
}
cout << "How many spools are in stock: ";
cin >> spoolsStock;
//Validate spools in stock
while(spoolsStock < 0)
{
cout << "Spools in stock must be at least 0" << endl;
cin >> spoolsStock;
}
cout << "Are there any special shipping charges? ";
cout << "Enter Y for yes or another letter for no: ";
cin >> ship;
//Validate special charges
if(ship == 'Y' || ship == 'y')
{
cout << "Enter the special shipping charge: $";
cin >> specialCharges;
}
else
{
specialCharges = 10.00;
}
}
void displayStatus(int &backOrder, double &subtotal, double &shipping, double &total)
{
}
推荐答案
您的声明和定义 getStockInfo
differ:最后一个参数是一个引用,而另一个则不是。
Your declaration and definition of getStockInfo
differ: The final parameter in one is a reference and in the other it is not.
void getStockInfo(int &, int&, double&);
...
void getStockInfo(int &spoolsOrdered, int &spoolsStock, double specialCharges)
displayStatus
会出现类似的问题:这里的参数数量不同。
A similar issue occurs with displayStatus
: Here the number of parameters is different.
void displayStatus(int &, double &);
...
void displayStatus(int &backOrder, double &subtotal, double &shipping, double &total)
错误消息出现,因为编译器无法确定是否要告诉它调用 getStockInfo(int& int& double; / code>(可能来自另一个文件)或在此文件中定义的一个
void getStockInfo(int& int& double)
。
The error message occurs since the compiler can't determine whether you are telling it to call getStockInfo(int &, int&, double&)
(which may come from another file) or the one defined in this file void getStockInfo(int &, int&, double)
.
注意有多个版本不是错误。但是调用一个编译器不知道要调用哪个。
Note having multiple versions is not "wrong". However calling one in a way that the compiler doesn't know which one to call is.
这篇关于c ++引用参数函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!