Visual Studio 2015“非标准语法;使用“&”创建一个指向成员的指针” [英] Visual Studio 2015 “non-standard syntax; use '&' to create a pointer to member”

查看:314
本文介绍了Visual Studio 2015“非标准语法;使用“&”创建一个指向成员的指针”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C ++,并尝试制作一个Tic Tac Toe的小游戏。但是我继续获得非标准语法C3867。使用&来创建要记住的指针。

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.

这是我的TicTacToe.h:

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;
};

这是我的TicTacToe.cpp:

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|\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?

推荐答案

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

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

Board.player1Move;

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

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:

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

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

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

这篇关于Visual Studio 2015“非标准语法;使用“&amp;”创建一个指向成员的指针”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆