Visual Studio 2015“;非标准语法;使用'&';创建指向成员”; 我正在学习C++,尝试制作一个小游戏。但我不断得到C3867,非标准语法;使用“&”创建要记住的指针

Visual Studio 2015“;非标准语法;使用'&';创建指向成员”; 我正在学习C++,尝试制作一个小游戏。但我不断得到C3867,非标准语法;使用“&”创建要记住的指针,c++,visual-studio-2015,C++,Visual Studio 2015,这是我的提克塔克。h: #pragma once #include <iostream> using namespace std; class TicTacToe { public: TicTacToe(); string getName1(); string getName2(); void printBoard(); void clearBoard(); void setName1(string player1Name);

这是我的提克塔克。h:

#pragma once
#include <iostream>

using namespace std;

class TicTacToe
{
public:
    TicTacToe();
    string getName1();
    string getName2();
    void printBoard();
    void clearBoard();
    void setName1(string player1Name);
    void setName2(string player2Name);
    void setSign1(string player1Sign);
    void setSign2(string player2Sign, string player1Sign);
    void playGame(string player1Name, string player2Name,string    player1Sign,string player2Sign);                  
    void player1Move(string coordX);
    void player1Turn();
    void player2Turn();
private:
    char Board[3][3];
    string _player1Name;
    string _player2Name;
    string _player1Sign;
    string _player2Sign;
    string _coordX;
    string _coordY;
};
#pragma一次
#包括
使用名称空间std;
提克塔克类
{
公众:
TicTacToe();
字符串getName1();
字符串getName2();
作废印刷板();
空挡板();
void setName1(字符串播放器1name);
void setName2(字符串播放器2名);
无效设置信号1(字符串播放器1信号);
无效设置信号2(弦乐演奏者2信号,弦乐演奏者1信号);
无效玩家游戏(字符串玩家1名称、字符串玩家2名称、字符串玩家1签名、字符串玩家2签名);
无效播放器1移动(字符串坐标);
void player1Turn();
void player2Turn();
私人:
炭板[3][3];
字符串\u player1名称;
字符串\u player2名称;
弦乐演奏者1号;
弦乐演奏者2号;
字符串coordX;
弦乐;
};
这是我的TicTacToe.cpp:

#include "TicTacToe.h"
#include <iostream>
#include <string>

TicTacToe::TicTacToe() {}

void TicTacToe::playGame(string player1Name, string player2Name,
                         string player1Sign, string player2Sign) {
  TicTacToe Board;
  Board.setName1(player1Name);
  Board.setSign1(player1Sign);
  Board.setName2(player2Name);
  Board.setSign2(player1Sign, player2Sign);
  Board.clearBoard();
  Board.printBoard();
}

void TicTacToe::printBoard() {
  cout << " |1|2|3|\n";
  for (int i = 0; i < 3; i++) {
    cout << "--------\n";
    cout << i + 1 << "|" << Board[i][0] << "|" << Board[i][1] << "|"
         << Board[i][2] << "|" << endl;
  }
}

void TicTacToe::clearBoard() {
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      Board[i][j] = ' ';
    }
  }
}

void TicTacToe::setName1(string player1Name) {
  cout << "Enter your name, player 1: \n";
  cin >> player1Name;
  _player1Name = player1Name;
}

void TicTacToe::setName2(string player2Name) {
  cout << "Enter your name, player 2: \n";
  cin >> player2Name;
  _player2Name = player2Name;
}

string TicTacToe::getName1() { return _player1Name; }

string TicTacToe::getName2() { return _player2Name; }

void TicTacToe::setSign1(string player1Sign) {
  cout << "What will you sign be?(X/O)\n";
  cin >> player1Sign;
  if (player1Sign != "X" && player1Sign != "O" && player1Sign != "x" &&
      player1Sign != "o") {
    cout << "Invalid input, try again.\n";
    cin >> player1Sign;
  }
  _player1Sign = player1Sign;
}

void TicTacToe::setSign2(string player2Sign, string player1Sign) {
  cout << "What will you sign be?(X/O)\n";
  cin >> player2Sign;

  if (player2Sign != "X" && player2Sign != "O" && player2Sign != "x" &&
          player2Sign != "o" ||
      player2Sign == player1Sign) {
    cout << "Invalid input, try again.\n";
    cin >> player2Sign;
  }
  _player2Sign = player2Sign;
}

void TicTacToe::player1Move(string coordX) // ERROR
{
  cout << "Enter X: " << endl;
  cin >> coordX;
  _coordX = coordX;
}

void TicTacToe::player1Turn() {
  cout << "Player 1 turn !\n";
  TicTacToe Board;
  Board.player1Move;
}

void TicTacToe::player2Turn() {
  cout << "Player 2 turn !\n";
  TicTacToe Board;
  Board.player1Move;
}
#包括“TicTacToe.h”
#包括
#包括
TicTacToe::TicTacToe(){}
void TicTacToe::playGame(字符串播放器1名称、字符串播放器2名称、,
弦乐演奏者1号,弦乐演奏者2号){
提克塔托板;
Board.setName1(player1Name);
板。设置信号1(播放器1信号);
Board.setName2(player2Name);
板设置信号2(player1信号,player2信号);
Board.clearBoard();
印制板();
}
void TicTacToe::printBoard(){

cout问题在于包含以下内容的行(在代码中出现两次):

Player one move是一个接收std::string参数作为输入的函数。要调用它,您需要创建一个std::string对象,并将其作为函数的参数传递。如果希望字符串作为输入提供,可以使用以下语法:

std::string move;
cin >> move;
Board.player1Move(move);

另外,请注意player2Turn应该调用Board.player2Move而不是Board.player1Move。

中已经提供了解决问题的方法。我将尝试解释错误消息

如果有非成员函数,则可以在表达式中使用函数名,而无需使用函数调用语法

void foo()
{
}

foo; // Evaluates to a function pointer.
但是,如果有成员函数,则在表达式中使用成员函数名而不使用函数调用语法是无效的

struct Bar
{
   void baz() {}
};

Bar::baz;  // Not valid.
要获取指向成员函数的指针,需要使用
&
运算符

&Bar::baz;   // Valid
这解释了来自Visual Studio的错误消息:

"non-standard syntax; use '&' to create a pointer to member"

我是在为
try/catch
代码块快速向应用程序添加
std::exception
时得到这个消息的,如下所示:

try{
//code block
}catch(std::exception e){printf("ERROR: %s", e.what()); return -1;}
我只需要将异常名称的参数更改为以
开头,如下所示:

try{
//code block
}catch(std::exception &e){printf("ERROR: %s", e.what()); return -1;}

请创建一个.Emphasis on minimal。您需要标记导致问题的行,以使我们的生活更轻松!当您假设将其作为函数调用时,您说的是
Board.player1Move
。此外,它还包含一个参数(字符串)为什么你没有通过?为什么C++中TigToToTi游戏突然涌入?甚至不是一个玩游戏的游戏!抱歉,第一次问问题,我会记住。谢谢你的回答。这是我的答案,因为我必须用代码<函数>替换一些代码,比如“代码>函数-TousSe= BAZ;< /Cord>”。se=&Bar::baz;
当我正在维护的某些软件停止编译时。
functionToUse
变量被分配了一个用于8位图形的函数或另一个用于24位彩色图形的函数。令人惊讶的是,如果类名前面没有范围解析运算符,则上述操作将不起作用,即仅
&baz
是非法的。
try{
//code block
}catch(std::exception &e){printf("ERROR: %s", e.what()); return -1;}