Ethan Shapiro Ethan Shapiro - 3 months ago 20
C++ Question

Visual Studio 2015 "non-standard syntax; use '&' to create pointer for member"

I'm creating a quick game and I used a different class file for inputting player names. I keep getting the specified error :


use '&' to create pointer for member


when I am trying to call the function from main.

The function
getPlayerOne
and
getPlayerTwo
are public functions. I think it's because I'm changing the
player1
value so I need a pointer but when I try to add a pointer it gives me the same error.

How do I edit values of strings using pointers?

main:

#include <iostream>
#include <string>

//Included Header Files
#include "Player.h"

using namespace std;

int main() {

Player players;
cout << players.getPlayerOne << endl;
cout << players.getPlayerTwo << endl;

}


Player.h:

#pragma once
#include <iostream>
#include <string>

using namespace std;

class Player
{
public:

//Initialize player1
void getPlayerOne(string &playerOne);
void getPlayerTwo(string &playerTwo);


Player();
private:

//Players
string player1;
string player2;
};


Player.cpp

#include "Player.h"

Player::Player()
{
}

void Player::getPlayerOne(string &playerOne) {
cout << "Enter player 1 name: \n";
cin >> playerOne;
cout << playerOne << " is a great name!\n";
player1 = playerOne;
}

void Player::getPlayerTwo(string &playerTwo) {
cout << "Enter player 2 name: \n";
cin >> playerTwo;
cout << playerTwo << " is a great name!\n";

player2 = playerTwo;
}


I could probably just put the
Player
code in main because it's so small, but I think it is better to have separate classes when (eventually) I can program files with more characters.

Answer

You have 2 function named getPlayerX(string & name) returning nothing : I think you made a mistake in what are "getters" and "setters".

You want your std::cout to GET a string, then make your functions getPlayerX() like a GETTER :

std::string getPlayerX() const noexcept;

You want to SET your data, make a SETTER :

void setPlayerX(const std::string & name);

Remember, the function you use are like this :

Type_you_want_to_get   Name_of_the_function ([const] type_of_argument [&] var_name)

void "means" return nothing.