C++ 将boost::bind成员函数传递给wt侦听器。。我做错了什么?
这个问题花了我几个小时,但我一点也不明白我做错了什么 这是编译错误消息: 错误113错误C2298:“返回”:对成员函数表达式c:\program files\wt 3.3.2 msvs2012 x86\include\boost\bind\mem\u fn.hpp 342 1的指针执行非法操作 错误位于mem_fn.hpp中的函数C++ 将boost::bind成员函数传递给wt侦听器。。我做错了什么?,c++,boost,wt,C++,Boost,Wt,这个问题花了我几个小时,但我一点也不明白我做错了什么 这是编译错误消息: 错误113错误C2298:“返回”:对成员函数表达式c:\program files\wt 3.3.2 msvs2012 x86\include\boost\bind\mem\u fn.hpp 342 1的指针执行非法操作 错误位于mem_fn.hpp中的函数dm::operator()中。我粘贴了文件(第342行) 我猜问题源于boost::bind(),但我检查了绑定的函数,它们似乎都有正确数量的参数 #include
dm::operator()
中。我粘贴了文件(第342行)
我猜问题源于boost::bind()
,但我检查了绑定的函数,它们似乎都有正确数量的参数
#include <Wt/WApplication>
#include <Wt/WPushButton>
#include <Wt/WTable>
#include <Wt/WEnvironment>
#include <Wt/WServer>
#include <vector>
using namespace Wt;
using std::vector;
using std::string;
enum Tile {
N, X, O
};
class TicTacToeGame;
class TicTacToeClient: public WApplication {
public:
TicTacToeClient(const WEnvironment& env);
void connectFrom(TicTacToeGame& game);
void setTile(Tile t);
void updateBoard(int, int, Tile);
private:
void takeMove(int i, int j);
Tile tile;
Signal<int, int, Tile>* moveTaken;
WTable* table;
};
class TicTacToeGame {
public:
static vector<TicTacToeGame*> games;
TicTacToeGame(TicTacToeClient* x): playerX(x) {
for(int i=0;i<3;i++){
vector<Tile> line;
for(int j=0;j<3;j++)
line.push_back(Tile::N);
grid.push_back(line);
}
playerX->connectFrom(*this);
playerX->setTile(Tile::X);
playerX->enableUpdates();
}
void updateBoard(int x, int y, Tile t){
playerX->environment().server()->post(playerX->sessionId(), boost::bind(&TicTacToeClient::updateBoard, playerX, x, y, t));
playerO->environment().server()->post(playerO->sessionId(), boost::bind(&TicTacToeClient::updateBoard, playerO, x, y, t));
}
void join(TicTacToeClient* o){
playerO = o;
playerO->connectFrom(*this);
playerO->setTile(Tile::O);
playerO->enableUpdates();
}
private:
vector<vector<Tile>> grid;
TicTacToeClient* playerX;
TicTacToeClient* playerO;
};
vector<TicTacToeGame*> TicTacToeGame::games = vector<TicTacToeGame*>();
TicTacToeClient::TicTacToeClient(const WEnvironment& env): WApplication(env), moveTaken(new Signal<int, int, Tile>){
WTable* table = new WTable();
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
WPushButton* button = new WPushButton();
button->setWidth(50);
button->setHeight(50);
button->clicked().connect(boost::bind(&TicTacToeClient::takeMove, this, i, j));
WTableCell* cell = table->elementAt(i,j);
cell->addWidget(button);
cell->setHeight(50);
cell->setWidth(50);
}
}
root()->addWidget(table);
this->table = table;
}
void TicTacToeClient::connectFrom(TicTacToeGame& game){
moveTaken->connect(boost::bind(&TicTacToeGame::updateBoard, &game));
}
void TicTacToeClient::setTile(Tile t){tile = t;}
void TicTacToeClient::updateBoard(int x, int y, Tile t){
WTableCell* cell = table->elementAt(x, y);
cell->clear();
string text;
switch(t){
case Tile::X:
text = "X";
break;
case Tile::O:
text = "O";
break;
}
cell->addWidget(new WText(text));
}
void TicTacToeClient::takeMove(int i, int j){
moveTaken->emit(i, j, tile);
}
WApplication *createApplication(const WEnvironment& env)
{
TicTacToeClient* client = new TicTacToeClient(env);
if(TicTacToeGame::games.empty())
TicTacToeGame::games.push_back(new TicTacToeGame(client));
else
TicTacToeGame::games[0]->join(client);
return client;
}
int main(int argc, char **argv)
{
return WRun(argc, argv, &createApplication);
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间Wt;
使用std::vector;
使用std::string;
枚举块{
N、 X,O
};
课堂游戏;
TictoeClient类:公共应用程序{
公众:
TictoeClient(const WenEnvironment&env);
无效连接(TictoEgame和game);
空心砖(砖t);
无效更新板(int,int,Tile);
私人:
无效移动(int i,int j);
瓷砖;
信号*;
WTable*表;
};
课堂游戏{
公众:
静态向量博弈;
TictoEgame(TictoEclient*x):playerX(x){
对于(inti=0;isetTile(Tile::X);
playerX->enableUpdates();
}
无效更新板(int x,int y,Tile t){
playerX->environment().server()->post(playerX->sessionId(),boost::bind(&TicTacToeClient::updateBoard,playerX,x,y,t));
playerO->environment().server()->post(playerO->sessionId(),boost::bind(&TicTacToeClient::updateBoard,playerO,x,y,t));
}
无效联接(TictoeClient*o){
playerO=o;
playerO->connectFrom(*此);
playerO->setTile(Tile::O);
playerO->enableUpdates();
}
私人:
矢量网格;
TictoeClient*playerX;
TictoeClient*playerO;
};
vector TictoEgame::games=vector();
TicTacToeClient::TicTacToeClient(const-WEnvironment&env):waapplication(env)、movetake(新信号){
WTable*表=新的WTable();
对于(int i=0;isetHeight(50);
按钮->点击().connect(boost::bind(&TicTacToeClient::takeMove,this,i,j));
WTableCell*cell=table->elementAt(i,j);
单元格->添加小部件(按钮);
单元->设置高度(50);
单元->设置宽度(50);
}
}
root()->addWidget(表);
这个->表格=表格;
}
void TicTacToeClient::connectFrom(TicTacToeGame&game){
movetake->connect(boost::bind(&TicTacToeGame::updateBoard,&game));
}
void TicTacToeClient::setTile(Tile t){Tile=t;}
void TicTacToeClient::updateBoard(int x,int y,Tile t){
WTableCell*cell=table->elementAt(x,y);
单元格->清除();
字符串文本;
开关(t){
案例平铺::X:
text=“X”;
打破
案例互动程序::O:
text=“O”;
打破
}
单元格->添加小部件(新的WText(文本));
}
void TicTacToeClient::takeMove(inti,intj){
movetake->emit(i,j,tile);
}
waapplication*createApplication(const-WEnvironment&env)
{
TicTacToeClient*客户端=新的TicTacToeClient(env);
if(TicTacToeGame::games.empty())
TictoEgame::games.push_back(新的TictoEgame(客户端));
其他的
TictoEgame::games[0]->加入(客户端);
返回客户;
}
int main(int argc,字符**argv)
{
返回WRun(argc、argv和createApplication);
}
我使用的是32位Win7,MSVS Express 2012,Wt是3.3.2,由“简易方法”安装。我认为您的问题源于此:
void TicTacToeClient::connectFrom(TicTacToeGame& game){
moveTaken->connect(boost::bind(&TicTacToeGame::updateBoard, &game));
}
您绑定了调用对象(&game),但该函数有3个参数。我不熟悉Wt,但看起来MoveTake信号将使用正确的参数调用该函数。在这种情况下,您只需要在boost::bind调用中添加占位符
moveTaken->connect(boost::bind(&TicTacToeGame::updateBoard, &game, _1, _2, _3));
但我对此不确定,所以请告诉我结果如何。我认为您的问题源于:
void TicTacToeClient::connectFrom(TicTacToeGame& game){
moveTaken->connect(boost::bind(&TicTacToeGame::updateBoard, &game));
}
您绑定了调用对象(&game),但该函数有3个参数。我不熟悉Wt,但看起来MoveTake信号将使用正确的参数调用该函数。在这种情况下,您只需要在boost::bind调用中添加占位符
moveTaken->connect(boost::bind(&TicTacToeGame::updateBoard, &game, _1, _2, _3));
虽然我对此不确定,但请告诉我结果如何。这就是问题所在。问题与完全相同。这让人尴尬。我很高兴我学到了一些东西,也很抱歉我浪费了你的时间。不过,程序仍然无法运行(当一个人玩了一个动作时没有更新),但这是另一个故事。这就是问题所在。问题与完全相同。这让人尴尬。我很高兴我学到了一些东西,也很抱歉我浪费了你的时间。尽管如此,程序仍然无法运行(当一个人移动时没有更新),但这是另一个故事。