2013-04-02 205 views
0

我的參考參數有問題。 getStockInfo中的值應該存儲在參考參數中。我不知道如何做到這一點,以便displayStatus接受這些作爲參數。每當我把一些東西放入getStockInfo,它會給我錯誤More than one onstance of overloaded function "getStockInfo" matches the argument listC++參考參數函數

#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

在你的代碼看getStockInfo'的'兩個地和比較。 – chris

+1

看看它們的函數原型,以及實際的定義。他們不平等。 –

回答

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)定義的一個。

注意有多個版本不是「錯誤的」。但是,以編譯器不知道要調用哪一個的方式調用它。

0

原型中的參數列表與定義中的參數列表不匹配。

void displayStatus(int &, double &); 

VS

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