2016-05-07 1 views
-4

"sales"という2次元配列を関数に渡すときに問題があります。私が間違っていることは何か考えていますか?私はオンラインクラスとしてC++を持っていないし、私の先生は何の助けではありません:(私は取得していますエラーメッセージは次のとおりです。2D配列をエラーメッセージなしの関数に渡すことができませんC++

オーバーロードされた関数のインスタンスを「getTotalは、」引数 リストだけでなく、「COLS」にマッチ:宣言されていない識別子

getTotalあなたはdefiniでいることを修正して、それが定義されており、あなたがそれを行うことができない前に、あなたがCOLSを使用している2つの引数「

// Week 7 Assignment 2 
#include <iostream> 
#include <iomanip> 
using namespace std; 

// Function Prototypes 
int getTotal(int[][COLS]); 


// Global Variables 
const int ROWS = 4; 
const int COLS = 4; 
const int NUM_DIVS = 5; 
const int NUM_QTRS = 5; 
int sales[ROWS][COLS]; 
int totalSales; 
string division[NUM_DIVS] = { "North", "South", "East", "West", "Quarter Total" }; 
string quarters[NUM_QTRS] = { "Quarter 1", "Quarter 2", "Quarter 3", "Quarter 4" }; 
int total; 

int main() 
{ 
    // Variables 

    cout << "This program will calculate information about sales during a year." << endl; 

    // Loops to fill the array 
    for (int count = 0; count < COLS; count++) 
    { 
     cout << "Please enter the sales for the North during Quarter " << (count + 1) << ": $"; 
     cin >> sales[0][count]; 
    } 
    for (int count = 0; count < COLS; count++) 
    { 
     cout << "Please enter the sales for the South during Quarter " << (count + 1) << ": $"; 
     cin >> sales[1][count]; 
    } 
    for (int count = 0; count < COLS; count++) 
    { 
     cout << "Please enter the sales for the East during Quarter " << (count + 1) << ": $"; 
     cin >> sales[2][count]; 
    } 
    for (int count = 0; count < COLS; count++) 
    { 
     cout << "Please enter the sales for the West during Quarter " << (count + 1) << ": $"; 
     cin >> sales[3][count]; 
    } 
    total = getTotal(sales, 4); 

    return 0; 

} 

// Function to get the total of everything in the array 
int getTotal(int sales[][COLS]) 
{ 
    int totAl = 0; 
    for (int count = 0; count < ROWS; count++) 
    { 
     for (int count = 0; count < COLS; count++) 
      totAl += sales[count][count]; 

     return totAl; 
    } 
} 
+0

変数を使用する前に宣言する必要があります。最初に 'COLS'を使い、宣言します。 – nwp

+0

あなたが言及したエラーとは別に、コードに別のバグがあります。あなたの関数は、最初の行の和を返します – user463035818

+0

[C++関数に2次元配列を渡す]可能な複製(http://stackoverflow.com/questions/8767166/passing-a-2d-array-to-ac-function ) –

答えて

0

ファーストをとらない機能 それを使用する前に、まずそれをngの:あなたのコンパイラが正しく指摘したように

// Global Variables 
const int ROWS = 4; 
const int COLS = 4;//this is defined here 

int getTotal(int[][COLS]);//now the compiler knows what COLS is 

第二に、あなたのgetTotal関数は、引数を1つしか受け入れていますがtotal = getTotal(sales, 4);に2つを渡しています。私はあなたが2番目の引数として行サイズを渡そうとしていると信じていますが、すべての関数はROWの定義がグローバル変数であることを見ることができます(実際にはgetTotal関数内にROWSを使用しています)。

total = getTotal(sales); //this is enough to call the function 

また、配列がグローバル宣言されているので、私はあなたがこの広範囲にあなたのコード内でグローバル変数を使用することをお勧めしませんが、あなたのgetTotal関数に任意のパラメータを渡す必要がないことに注意してください。

int getTotal();//function prototype without parameters since sales array is global variable 
total = getTotal(); //call the function 

はまた、予期しない結果につながる、あなたのループカウンタの両方と同じ変数countを定義するように、現在の実装以来getTotalの内側にあなたのコードのロジックを再検討する必要があるかもしれません。

+0

ありがとうbkVnet ....私はあなたのおかげで今すぐ機能しています。この割り当てにはさらに5つの関数が必要ですが、同じテクニックを適用してそれらを動作させることを願っています。私は本当にそれを感謝します。 –

関連する問題