C++ Visual Studio 2015”非标准语法;使用“&”创建一个指向成员的指针”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37815641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:43:28  来源:igfitidea点击:

Visual Studio 2015 “non-standard syntax; use '&' to create a pointer to member”

c++visual-studio-2015

提问by T. Long

I am learning C++ and try to make a small game of Tic Tac Toe. But I keep on getting C3867, non-standard syntax; use '&' to create a pointer to remember.

我正在学习 C++ 并尝试制作一个 Tic Tac Toe 小游戏。但我不断得到 C3867,非标准语法;使用“&”创建一个要记住的指针。

This is my TicTacToe.h :

这是我的 TicTacToe.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;
};

And here is my TicTacToe.cpp :

这是我的 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;
}

I have tried everything in other questions about this error but they didn't work. How do you fix this error?

我已经尝试了有关此错误的其他问题中的所有内容,但没有奏效。你如何解决这个错误?

采纳答案by ibezito

The problem is with the lines which contain the following (it appears twice in your code):

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

Board.player1Move;

Player one move is a function which receive an std::string parameter as an input. In order to call it you'll need to create an std::string object and pass it as an argument for the function. You can use the following syntax if you want the string to be given as an input:

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

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

Also, notice that player2Turn should call Board.player2Move instead of Board.player1Move.

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

回答by R Sahu

The fix to your problem is already provided in the answer by drorco. I am going to try to explain the error message.

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

When you have a non-member function, you can use the function name in an expression without using the function call syntax.

当您有一个非成员函数时,您可以在表达式中使用函数名而不使用函数调用语法。

void foo()
{
}

foo; // Evaluates to a function pointer.

However, when you have a member function, using the member function name in an expression without the function call syntax is not valid.

但是,当您有成员函数时,在没有函数调用语法的表达式中使用成员函数名称是无效的。

struct Bar
{
   void baz() {}
};

Bar::baz;  // Not valid.

To get a pointer to a member function, you need to use the &operator.

要获得指向成员函数的指针,您需要使用&运算符。

&Bar::baz;   // Valid

That explains the error message from Visual Studio:

这解释了来自 Visual Studio 的错误消息:

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

回答by kayleeFrye_onDeck

I was getting this when quickly adding std::exceptionto an app for try/catchblocks of code, like this:

当快速添加std::exception到应用程序try/catch的代码块时,我得到了这个,如下所示:

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

I just needed to change parameter for the exception's name to start with &, like so:

我只需要更改异常名称的参数以开头&,如下所示:

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