2017-11-04 63 views
-4

我只編寫了幾個月的代碼,並且遇到了我的任務之一的障礙。該描述是傳遞引用程序C++

「重寫與函數給出的錢包porgram執行插入和 刪除操作。函數初始化(INT & P,詮釋& N,詮釋& d, INT & Q)將初始化便士,鎳,角錢和宿舍爲零。 功能插入(INT & p,詮釋& N,詮釋& d,詮釋& q)將增加便士, 五分,一角硬幣和宿舍。函數美元()將返回 美元。函數remove(int & p,int & n,int & d,int & q)將 減去便士,鎳幣,硬幣和四分之一。該功能顯示() 返回一個新字符串打印錢包的剩餘 便士,鎳,角錢和宿舍的內容。」

這是我已經能夠拿出這麼遠。

#include <iostream> 

using namespace std; 

int main() 
{ 
    int pennies, nickels, dimes, quarters, p, n, d, q; 
    float total; 

    void initialize(int&, int&, int&, int&); 
    void insert(int&, int&, int&, int&); 
    void remove(int&, int&, int&, int&); 
    float dollars(int, int, int, int); 
    void print(int, int, int, int, float); 


    initialize(pennies, nickels, dimes, quarters); 
    total = dollars(pennies, nickels, dimes, quarters); 
    print(pennies, nickels, dimes, quarters, total); 

    insert(pennies, nickels, dimes, quarters); 
    total = dollars(pennies, nickels, dimes, quarters); 
    print(pennies, nickels, dimes, quarters, total); 

    remove(pennies, nickels, dimes, quarters); 
    total = dollars(pennies, nickels, dimes, quarters); 
    print(pennies, nickels, dimes, quarters, total); 

} 
void initialize(int& pennies, int& nickels, int& dimes, int& quarters) 

{ 
    pennies=0; 
    nickels=0; 
    dimes=0; 
    quarters=0; 
} 

void insert(int& pennies, int& nickels, int& dimes, int& quarters) 
{ 
    int p, n, d, q; 
    cout << "Insert pennies, nickels, dimes, and quarters:"; 
    cin >> p, n, d, q; 

    pennies +=p; 
    nickels +=n; 
    dimes +=d; 
    quarters +=q; 

} 

void remove(int& pennies, int& nickels, int& dimes, int& quarters) 
{ 
    int p, n, d, q; 
    cout << "Remove pennies, nickels, dimes, and quarters:"; 
    cin >> p, n, d, q; 

    pennies -=p; 
    nickels -=n; 
    dimes -=d; 
    quarters -=q; 

} 

float dollars(int pennies, int nickels, int dimes, int quarters, float total) 
{ 
    total= pennies + 5*nickels + 10*dimes + 25*quarters; 
    total=(float)total/100; 

    return total; 
} 
void print(int pennies, int nickels, int dimes, int quarters, float total) 
{ 
    cout << quarters << "quarters+ " << dimes << " dimes+" << nickels << " nickels+" << pennies << " pennies= $" << total <<"\n"; 

} 

我一直在使用onlinegdb.com被編譯我的所有程序因爲這是我所有的類需要我使用。當我編譯這個程序,我得到

/tmp/ccd19JQt.o: In function `main': 
main.cpp:(.text.startup+0x2b): undefined reference to `dollars(int, int, int, int)' 
main.cpp:(.text.startup+0x6a): undefined reference to `dollars(int, int, int, int)' 
main.cpp:(.text.startup+0xa9): undefined reference to `dollars(int, int, int, int)' 
collect2: error: ld returned 1 exit status 

d任何人都知道我可以做些什麼來讓程序編譯和運行?非常感謝。

這就是我爲什麼改變它以使其工作。感謝您的建議。

int main() 
{ 
    int pennies, nickels, dimes, quarters, p, n, d, q; 
    float total; 

    void initialize(int&, int&, int&, int&); 
    void insert(int&, int&, int&, int&); 
    void remove(int&, int&, int&, int&); 
    float dollars(int, int, int, int); 
    void print(int, int, int, int, float); 


    initialize(pennies, nickels, dimes, quarters); 
    total = dollars(pennies, nickels, dimes, quarters); 
    print(pennies, nickels, dimes, quarters, total); 

    insert(pennies, nickels, dimes, quarters); 
    total = dollars(pennies, nickels, dimes, quarters); 
    print(pennies, nickels, dimes, quarters, total); 

    remove(pennies, nickels, dimes, quarters); 
    total = dollars(pennies, nickels, dimes, quarters); 
    print(pennies, nickels, dimes, quarters, total); 

} 
void initialize(int& pennies, int& nickels, int& dimes, int& quarters) 

{ 
    pennies=0; 
    nickels=0; 
    dimes=0; 
    quarters=0; 
} 

void insert(int& pennies, int& nickels, int& dimes, int& quarters) 
{ 
    int p, n, d, q; 
    cout << "Insert pennies, nickels, dimes, and quarters:"; 
    cin >> p>>n>>d>>q; 

    pennies +=p; 
    nickels +=n; 
    dimes +=d; 
    quarters +=q; 

} 

void remove(int& pennies, int& nickels, int& dimes, int& quarters) 
{ 
    int p, n, d, q; 
    cout << "Remove pennies, nickels, dimes, and quarters:"; 
    cin >> p>>n>>d>>q; 

    pennies -=p; 
    nickels -=n; 
    dimes -=d; 
    quarters -=q; 

} 

float dollars(int pennies, int nickels, int dimes, int quarters) 
{ 
    float total; 
    total= pennies + 5*nickels + 10*dimes + 25*quarters; 
    total=(float)total/100; 

    return total; 
} 
void print(int pennies, int nickels, int dimes, int quarters, float total) 
{ 
    cout << quarters << "quarters+ " << dimes << " dimes+" << nickels << " nickels+" << pennies << " pennies= $" << total <<"\n"; 

} 
+1

你的方法聲明在main中。將它們移出。 –

+1

爲什麼你將'float total'添加到'dollars'的參數列表中?它應該是你如何使用它的一個局部變量。不是參數。 – StoryTeller

+1

@ DanielA.White - 這是合法的,雖然古怪。 – StoryTeller

回答

0

以下是聲明:

float dollars(int, int, int, int); 
//   1 2 3 4 

您已經聲明瞭4個參數。

下面是函數調用:

total = dollars(pennies, nickels, dimes, quarters); 
//    1  2  3  4 

您傳遞4個參數。到現在爲止還挺好。

這裏的定義是:

float dollars(int pennies, int nickels, int dimes, int quarters, float total) 
//   1   2   3   4    5 
{ ... 

你給了一個定義5參數功能dollars(int,int,int,int,float)。鏈接器錯誤是因爲您調用的dollars(int,int,int,int)缺少定義,但沒有定義。

解決方案:定義功能dollars(int,int,int,int)

+0

感謝您的幫助。我設法讓你的幫助程序工作。 – Matthew