Ethan Shapiro Ethan Shapiro - 1 year ago 269
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
are public functions. I think it's because I'm changing the
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?


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



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

using namespace std;

class Player

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


string player1;
string player2;


#include "Player.h"


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
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download