gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 22:23:46 35 4
gpt4 key购买 nike

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

这是我的井字游戏.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;
};

这是我的井字游戏.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;
}

我已经在其他有关此错误的问题中尝试了所有方法,但没有奏效。您如何解决此错误?

最佳答案

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

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

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"

关于c++ - Visual Studio 2015 “non-standard syntax; use ' &' to create a pointer to member”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37815641/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com