Board.hというReversi.hをinclude したヘッダをBoardTest.cppというファイルにインクルードして、簡易オセロを作りたく、ターミナルで
g++ BoardTest.cpp
と打ち、コンパイルを試みました。すると以下のエラーが出ます。
Undefined symbols for architecture x86_64: "Board::move(Point const&)", referenced from: _main in BoardTest-07905c.o "Board::pass()", referenced from: _main in BoardTest-07905c.o "Board::undo()", referenced from: _main in BoardTest-07905c.o "Board::Board()", referenced from: ConsoleBoard::ConsoleBoard() in BoardTest-07905c.o "Board::isGameOver() const", referenced from: _main in BoardTest-07905c.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
リンクでエラーが出ているのはわかるのですが、これは上記のヘッダファイルとcppがリンクされていないということでしょうか?
また、なぜそうなっているのでしょうか?
それぞれのコードは以下です。
Board.h #ifndef REVERSI_H_INCLUDED #define REVERSI_H_INCLUDED #include <string> #include <sstream> #include <stdexcept> const int BOARD_SIZE = 8; const int MAX_TURNS = 60; typedef int Color; const Color EMPTY = 0; const Color WHITE = -1; const Color BLACK = 1; const Color WALL = 2; struct Point { int x; int y; Point() { Point(0, 0); } Point(int x, int y) { this->x = x; this->y = y; } Point(std::string coordstr) noexcept(false) { if(coordstr.length() < 2) throw "The argument must be Reversi style coordinates!"; x = coordstr[0] - 'a'+1; y = coordstr[1] - '1'+1; } operator std::string() const { std::ostringstream oss; oss << static_cast<char>('a'+x-1) << static_cast<char>('1'+y-1); return oss.str(); } bool operator==(const Point& p) const { if(x != p.x) return false; if(y != p.y) return false; return true; } }; /* bool operator==(const Point& lhs, const Point& rhs) { if(lhs.x != rhs.x) return false; if(lhs.y != rhs.y) return false; return true; }*/ struct Disc : public Point { Color color; Disc() : Point(0, 0) { color = EMPTY; } Disc(int x, int y, Color color) : Point(x, y) { this->color = color; } }; template<typename T> class ColorStorage { T data[3]; public: T& operator[](int color) { return data[color+1]; } const T& operator[](int color) const { return data[color+1]; } ColorStorage<T>& operator +=(const ColorStorage<T> &src) { data[0] += src.data[0]; data[1] += src.data[1]; data[2] += src.data[2]; return *this; } }; #endif
BoardTest.cpp
#include <iostream> #include "Board.h" using namespace std; class ConsoleBoard : public Board { public: void print() { cout << " a b c d e f g h " << endl; for(int y=1; y<=8; y++) { cout << " " << y; for(int x=1; x<=8; x++) { switch(getColor(Point(x, y))) { case BLACK: cout << "●"; break; case WHITE: cout << "◯"; break; default: cout << " "; break; } } cout << endl; } } }; ostream& operator<<(ostream& os, const Point& p) { string s = p; os << s; return os; } int main() { ConsoleBoard board; while(true) { board.print(); cout << "黒石" << board.countDisc(BLACK) << " "; cout << "白石" << board.countDisc(WHITE) << " "; cout << "空きマス" << board.countDisc(EMPTY) << endl; vector<Point> movables = board.getMovablePos(); for(unsigned i=0; i<movables.size(); i++) { cout << movables[i] << " "; } cout << endl << endl; cout << "手を入力してください"; Point p; string in; cin >> in; if(in == "p") { if(!board.pass()) cerr << "パスできません" << endl; continue; } if(in == "u") { // undo board.undo(); continue; } try { Point parse(in); p = parse; } catch(const char* e) { cerr << "リバーシ形式の手を入力してください!" << endl; continue; } if(board.move(p) == false) { cerr << "そこには置けません" << endl; continue; } if(board.isGameOver()) { cout << "----------------ゲーム終了----------------" << endl; return 0; } } return 0; }
Board.h
#ifndef BOARD_H_INCLUDED #define BOARD_H_INCLUDED #include "Reversi.h" #include <vector> class Board { public: Board(); void init(); bool move(const Point& point); bool pass(); bool undo(); bool isGameOver() const; unsigned countDisc(Color color) const { return Discs[color]; } Color getColor(const Point& p) const { return RawBoard[p.x][p.y]; } const std::vector<Point>& getMovablePos() const { return MovablePos[Turns]; } std::vector<Disc> getUpdate() const { if(UpdateLog.empty()) return std::vector<Disc>(); else return UpdateLog.back(); } Color getCurrentColor() const { return CurrentColor; } unsigned getTurns() const { return Turns; } std::vector<Point> getHistory() const { std::vector<Point> history; for(unsigned i=0; i<UpdateLog.size(); i++) { std::vector<Disc> update = UpdateLog[i]; if(update.empty()) continue; history.push_back(update[0]); } return history; } unsigned getLiberty(const Point& p) const { return Liberty[p.x][p.y]; } private: static const unsigned NONE = 0; static const unsigned UPPER = 1; static const unsigned UPPER_LEFT = 2; static const unsigned LEFT = 4; static const unsigned LOWER_LEFT = 8; static const unsigned LOWER = 16; static const unsigned LOWER_RIGHT = 32; static const unsigned RIGHT = 64; static const unsigned UPPER_RIGHT = 128; Color RawBoard[BOARD_SIZE+2][BOARD_SIZE+2]; unsigned Turns; Color CurrentColor; std::vector<std::vector<Disc> > UpdateLog; std::vector<Point> MovablePos[MAX_TURNS+1]; unsigned MovableDir[MAX_TURNS+1][BOARD_SIZE+2][BOARD_SIZE+2]; unsigned Liberty[BOARD_SIZE+2][BOARD_SIZE+2]; ColorStorage<unsigned> Discs; void flipDiscs(const Point& point); unsigned checkMobility(const Disc& disc) const; void initMovable(); }; #endif
回答2件
あなたの回答
tips
プレビュー