ユーザーが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ボードサイズにグローバル変数を使用するよう指示されました。
'othelloboard'クラスはどこにもインスタンス化していませんか? –
generateBoardのコードをコンストラクタに移動し、そのメソッドを削除します。これは、より自然な使用方法になります。 –
また、 '(char)i'はあなたが望むことをしません。 –