与多个类共享一个对象
sharing an object with multiple classes
>我有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);
}
相关文章:
- 为什么我不能将一个对象push_back到属于另一个类的对象向量中?
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- 从多个源构造一个对象,包括一个对象向量
- 为什么C++在将一个对象复制到另一个对象时需要对这两个对象进行低级常量限定
- 检查哪个对象调用了另一个对象的对象方法
- C++ 如何在将新对象分配给另一个对象时创建新对象
- 如何处理从一个对象传递到另一个在C++中具有公共抽象类的对象的消息
- 我有一个对象,它将在整个程序的持续时间内实例化,但一个类成员不会,我应该动态分配它吗?
- 包装一个对象并假装它是一个 int
- 为什么我可以在不重载 "=" 运算符的情况下将一个对象分配给另一个对象?
- 如何在qt中将信号和插槽与另一个对象连接 --解决了
- 模板函数如何处理可能共享一个交集的多个类型名称?
- 如何使用函数将一个对象的输入复制到另一个对象中
- C++:在另外两个对象之间共享一个对象
- 具有多个继承共享一个资源的对象 - 寻找良好的设计模式
- 复制一个对象并使两者共享一个成员变量 (C++)
- C++在对象的多个实例之间共享一个变量
- C 模板功能,相同类型,多个实例:共享一个相同的代码副本?即使在不同的CPP/对象文件中
- 全局共享类的一个对象/实例
- 与多个类共享一个对象