2016-11-27 8 views
0

ユーザーがothelloでお互いにプレイできる基本的なプログラムを作ろうとしていますが、今は単に空の配列を初期化しようとしていますボードを表示してください。私はクラスを数回使っていますが、私のコードは私が作った他のクラスとまったく違うようには見えませんが、なぜ私の関数が正しく宣言されていないのかわかりません。エラーは以下のとおりです。ここ関数はC++でクラスを使用するときに 'このスコープで宣言されていません'

othello.cpp: In function ‘int main()’: 
othello.cpp:9:17: error: ‘generateBoard’ was not declared in this scope 
    generateBoard(); 
      ^
othello.cpp:10:13: error: ‘addBorder’ was not declared in this scope 
    addBorder(); 
     ^
othello.cpp:11:18: error: ‘displayOthello’ was not declared in this 
    scope 
    displayOthello(); 
      ^
make: *** [othello.o] Error 1 

はここ

const int boardSize = 4; 

class othelloboard{ 
    public: 
    othelloboard(); 
    ~othelloboard(); 
    void generateBoard(); 
    void addBorder(); 
    void displayOthello(); 

    private: 
    char othelloArray[boardSize][boardSize]; 
}; 

othelloboard.hであることはここで

#include <iostream> 
using namespace std; 

#include "othelloboard.h" 

//Initialize global variables 
extern const int boardSize; 

othelloboard::othelloboard(){} 
othelloboard::~othelloboard(){} 

void othelloboard::generateBoard(){ 
    for (int i=1; i<= boardSize; i++){ 
    for (int j=1; j<= boardSize; j++){ 
     othelloArray[i][j] = '.'; 
    } 
    } 
} 

void othelloboard::addBorder(){ 
    for (int i=1; i<= boardSize; i++){ 
    char temp = (char)i + '0'; 
    othelloArray[0][i] = temp; 
    othelloArray[i][0] = temp; 
    } 
} 

void othelloboard::displayOthello(){ 
    for (int i=0; i<= boardSize; i++){ 
    for (int j=0; j<= boardSize; j++){ 
     cout << othelloArray[i][j]; 
    } 
    cout << endl; 
    } 
} 

は私も知っているOthello.cpp

#include <iostream> 
using namespace std; 

#include "othelloboard.h" 

int main(){ 
    extern const int boardSize; 
    cout << boardSize << endl; 
    generateBoard(); 
    addBorder(); 
    displayOthello(); 
} 

あるothelloboard.cppですグローバル変数は最大ではない、bボードサイズにグローバル変数を使用するよう指示されました。

+1

'othelloboard'クラスはどこにもインスタンス化していませんか? –

+0

generateBoardのコードをコンストラクタに移動し、そのメソッドを削除します。これは、より自然な使用方法になります。 –

+0

また、 '(char)i'はあなたが望むことをしません。 –

答えて

0

othelloboard.cppに実装されたメソッドを呼び出すことができるようにするために、あなたは、あなたが単にobjectName.methodName()を使用してメソッドを呼び出すことができ、main()内タイプothelloboardのオブジェクトを作成する必要があります。基本的には、次の操作を実行する必要があります。

  1. タイプothelloboardのオブジェクトを作成します。

    othelloboard ob;

  2. など

    ob.generateBoard();

を使用してメソッドを呼び出す

関連する問題