c ++参考参数函数

时间:2013-04-02 03:06:54

标签: c++ function parameters reference

我的参考参数有问题。 getStockInfo中的值应该存储在参考参数中。我不知道如何做到这一点,以便displayStatus接受那些作为参数。每当我在主getStockInfo中添加某些内容时,它会向我显示错误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)
{
}  

2 个答案:

答案 0 :(得分:1)

您对getStockInfo的声明和定义有所不同:一个中的最后一个参数是参考,另一个参数不是。

void getStockInfo(int &, int&, double&);
...
void getStockInfo(int &spoolsOrdered, int &spoolsStock, double specialCharges)

displayStatus出现类似问题:此处参数的数量不同。

void displayStatus(int &, double &);
...
void displayStatus(int &backOrder, double &subtotal, double &shipping, double &total)

出现错误消息,因为编译器无法确定您是否要告诉它调用getStockInfo(int &, int&, double&)(可能来自其他文件)或此文件void getStockInfo(int &, int&, double)中定义的文件。

注意有多个版本并不是“错误”。但是,以编译器不知道调用哪一个的方式调用一个。

答案 1 :(得分:0)

原型中的参数列表与定义中的参数列表不匹配。

void displayStatus(int &, double &);

VS

void displayStatus(int &backOrder, double &subtotal, double &shipping, double &total)
{
}