与多个类共享一个对象

sharing an object with multiple classes

本文关键字:共享 一个对象      更新时间:2023-10-16

>我有3个类,即棋盘,游戏和AI

我想让棋盘类的对象棋盘供游戏和 AI 使用,也由棋盘类使用。我希望他们访问单个棋盘对象(共享)

问题是,它给了我一个臭名昭著的致命错误LNK1169:找到一个或多个乘法定义的符号。此外,还有Board.h,Game.h和AI.h(其中只有声明),我也有它们相应的.cpp文件。每个.h文件都包含保护(#ifndef _XXXXXX_H_)

我试图将棋盘包含在 Board.h 文件中(就在类的下方),但似乎警卫不起作用。

Error   7   error LNK2005: "class Board chessBoard" (?chessBoard@@3VBoard@@A)     already defined in AI.obj C:UsersxxxxDocumentsVisual Studio 2010ProjectsCHESSv3CHESSv3Board.obj   CHESSv3
Error   8   error LNK2005: "class Board chessBoard" (?chessBoard@@3VBoard@@A)     already defined in AI.obj C:UsersxxxxxDocumentsVisual Studio 2010ProjectsCHESSv3CHESSv3Game.obj   CHESSv3
Error   9   error LNK2005: "class Board chessBoard" (?chessBoard@@3VBoard@@A) already defined in AI.obj C:UsersxxxxxDocumentsVisual Studio 2010ProjectsCHESSv3CHESSv3ProgramEntryPoint.obj  CHESSv3

人工智能

#ifndef _AI_H_
#define _AI_H_
#include <iostream>
#include <string>
using namespace std;
struct node {
    string position;
    string nextPosition;
    float score;
    int level;
    float totalscore;
    node* up;
    node* right;
    node* left;
    bool approved;
    string move;
};
class AI {
private:
    //string board;
    //string board[8][8];
    int score1;
    int maxscore;
    int totalscore;
public: 
    void GetBoard(string[][8]);
    void AnalyzeMyPositions();
    void ExecuteAdvanceHeuristicMove();
};
#endif

游戏.h

#ifndef _GAME_H_
#define _GAME_H_
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
class Game {
public:
    char WhosTurn();
    bool Playable();
    bool GetMoveFromPlayer();
    void TurnOver();
    Game();
private:
    char turn;
};
#endif

董事会.h

#ifndef _BOARD_H_
#define _BOARD_H_
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
class Board {
public:
    bool SquareChecker(string);
    bool MoveChecker(string);
    Board();
    void PrintBoard();
    int Execute(string);
    void UnExecute();
    string CB[8][8];
private:
    char turn;
    vector<string> BoardRecord;
    stack<string> CBR;
    //string CB[8][8];
};
Board chessBoard;
#endif

如果你真的想这样做,你需要在标题 Board.h 中extern声明对象:

extern Board chessBoard;

然后,您在董事会中提供一份声明.cpp:

Board chessBoard;

更好的做法是在封闭代码中创建它并将其(通过引用)传递给其他类的构造函数。

您正在寻找的是单例设计模式,可以通过以下方式实现:

// Board.h
class Board {
  private:
    static instance_;
  public:
    static Board *instance();
}
// Board.cpp
Board *Board::instance_ = NULL;
Board *Board::instance() {
  if (!instance_)
    instance_ = new Board();
  return instance_;
}

请注意,这种模式可以被视为好或坏,如果您不喜欢使用它,您应该考虑将实例化Board类的引用传递给所有需要的类,并将其作为实例变量存储在每个对象中。像这样:

Game::Game() {
  this->board = new Board();
  this->ai = new AI(board);
  // or better this->ai = new AI(this) so AI can access all game methods
}

您的问题可能是可能有 3 个不同的棋盘定义,因为您可能会添加 3 个不同的 #include"Board.h"。请只在你拥有更多控制权的地方制作一个对象,而不是在 Board.h 中全局创建它。

你试过这样吗?仅在.cpp文件中包含必要的包含声明。

//Board.h
class Board {};
//Game.h
class Board;
class Game {
    Board* myBoard;
public:
    void setBoard(Board*);
};
//AI.h
class Board;
class AI {
    Board* myBoard;
public:
    void setBoard(Board*);
};
void main() {
    Board chessBoard;
    Game g;
    g.setBoard(&chessBoard);
    AI ai;
    ai.setBoard(&chessBoard);
}