2017-04-05 3 views
-4

イントロコーディングクラスの課題を完了する際に問題が発生しました。私はこのスコープで宣言されていなかった、「[エラー] 『displayBills』のコンパイルエラーを得続ける。私は自分のコードを添付し、任意の提案をいただければ幸いです、ありがとう!スコープ内で宣言する際にエラーが発生しました

#include <iostream> 
#include <cstdlib> 
using namespace std; 
int main() 
{ 
int dollars; 
cout << "Please enter the whole dollar amount (no cents!). Input 0 to terminate: "; 
cin >> dollars; 
while (dollars != 0) 
    { 
    displayBills(dollars); 
    cout << "Please enter the a whole dollar amount (no cents!). Input 0 to terminate: "; 
    cin >> dollars; 
    } 
return 0; 
} 

displayBills(int dollars) 
{ 
int ones; 
int fives; 
int tens; 
int twenties; 
int temp; 

twenties = dollars/20; 
temp = dollars % 20; 
tens = temp/10; 
temp = temp % 10; 
fives = temp/5; 
ones = temp % 5; 

cout << "The dollar amount of ", dollars, " can be represented by the following monetary denominations"; 
cout << "  Twenties: " << twenties; 
cout << "  Tens: " << tens; 
cout << "  Fives: " << fives; 
cout << "  Ones: " << ones; 
} 
+0

定義順/前方宣言。 Btwでは、初期化されていない変数の長いリストを作成しないでください。 – LogicStuff

+0

コンパイラがあなたのプログラムテキストを上から下に正確に1回読むとします。 displayBills()を呼び出すと、その関数の宣言や定義はまだ見られません。この問題を解決するには、displayBills()関数の定義をmain(...)関数の定義の前に置きます。 –

答えて

0

あなたは前方を指定していませんあなたのdisplayBills関数の宣言。あなたは1を指定するか、またはそれへの呼び出しの前に、あなたの関数を置く必要があります。

0

機能mainで、あなたは機能displayBillsを呼び出して、まだそれが宣言されているため、コンパイラは、(この時点では、この機能を知りません/ファイルの後ろに定義されています)。

Eithe Rあなたの関数maindisplayBills(int dollars) { ...の定義を置く、または関数main前に少なくともこの関数の前方宣言を置く:

displayBills(int dollars); // Forward declaration; implementation may follow later on; 
// Tells the compiler, that function `displayBills` takes one argument of type `int`. 
// Now the compiler can check if calls to function `displayBills` have the correct number/type of arguments. 

int main() { 
    displayBills(dollars); // use of function; signature is now "known" by the compiler 
} 

displayBills(int dollars) { // definition/implementation 
    ... 
} 

ところで:あなたが世話をする必要がありますあなたのコード内のいくつかの問題があり、例えばusing namespace stdは意図しない名前の衝突のために通常危険です。関数は明示的な戻り値の型(またはvoid)でなければなりません。

0

他の人があなたの問題を助けるでしょう。しかし、また、あなたはあなたが

(displayBills.hを含めることを忘れていけない)関数displayBillsを定義しますdisplayBills.cppのCPPファイルを持つことができdisplayBills.hと呼ばれるヘッダファイルにdisplayBillsを宣言し、

#ifndef DISPLAYBILLS_H_INCLUDED 
#define DISPLAYBILLS_H_INCLUDED 

displayBills(int dollars); 
#endif DISPLAYBILLS_H_INCLUDED 

#include "displayBills.h" 

メイン機能の下から独自のcppファイルに移動するだけです。主な機能の上にヘッダーファイルが含まれています。

これは、すべての関数をメインにジャムするのではなく、どの関数がプロジェクトのどこにあるのかを簡単に知ることができるためです。

関連する問題