Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

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

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.

This is my 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 :

#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|
";
  for (int i = 0; i < 3; i++) {
    cout << "--------
";
    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: 
";
  cin >> player1Name;
  _player1Name = player1Name;
}

void TicTacToe::setName2(string player2Name) {
  cout << "Enter your name, player 2: 
";
  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)
";
  cin >> player1Sign;
  if (player1Sign != "X" && player1Sign != "O" && player1Sign != "x" &&
      player1Sign != "o") {
    cout << "Invalid input, try again.
";
    cin >> player1Sign;
  }
  _player1Sign = player1Sign;
}

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

  if (player2Sign != "X" && player2Sign != "O" && player2Sign != "x" &&
          player2Sign != "o" ||
      player2Sign == player1Sign) {
    cout << "Invalid input, try again.
";
    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 !
";
  TicTacToe Board;
  Board.player1Move;
}

void TicTacToe::player2Turn() {
  cout << "Player 2 turn !
";
  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?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...