1

I'm new to C++ and pretty new to coding in general, I'm trying to make a simple text based RPG for a bit of fun. I've made a class player, and I'm trying to edit it so that the user edits the name of the playerName but I'm getting errors and I really don't know why.

My main method:

// Thomas' Game.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include "Player.h"
#include <fstream>
#include <istream>


using namespace std;

int main()
{
    Player player;

    cout << "Hello traveller, could I ask you your name?";
    cin >> player.setName;
    cout << "Hello " << player.getName << endl;


return 0;
}

My player class: .h

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

using namespace std;

class Player
{
private:
    string playerName = "";
    string playerRace = "";
    int playerAge = 0;


public:
    void setName(std::string);
    string getName();
    void setRace(std::string);
    string getRace();
    void setAge(int);
    int getAge();
};

.ccp

#include "stdafx.h"
#include "Player.h"

void Player::setName(std::string name)
{
     playerName = name;
}
std::string Player::getName() {
     return playerName;
}
void Player::setAge(int age) 
{
    playerAge = age;
}
int Player::getAge() {
    return playerAge;
}
void Player::setRace(std::string race)
{
    playerRace = race;
}
std::string Player::getRace() {
    return playerRace;
}

Severity Code Description Project File Line Suppression State
Error C2679 binary '>>': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion) Thomas' Game.cpp 18

Severity Code Description Project File Line Suppression State
Error C3867 'Player::getName': non-standard syntax; use '&' to create a pointer to member Thomas' Game c:\users\thomas\source\repos\thomas' game\thomas' game\thomas' game.cpp 19

are the errors I'm currently getting

O'Neil
  • 3,790
  • 4
  • 16
  • 30
Tomhass
  • 13
  • 4
  • You are missing parentheses: Replace `player.getName` by `player.getName()` in line 18 – Tobias Ribizel Jan 13 '18 at 23:31
  • `int getAge();` -> `int getAge() const; perhaps – Ed Heal Jan 13 '18 at 23:32
  • Ditto with `getName` Also `void setName(std::string);` -> `void setName(const & std::string);` to avoid the copy overhead – Ed Heal Jan 13 '18 at 23:34
  • 4
    I'd say you need [a good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and start over from the beginning with that. – Some programmer dude Jan 13 '18 at 23:35
  • The compiler messages are in the output tab. –  Jan 13 '18 at 23:35
  • @EdHeal, if I do anything with const I get the error Severity Code Description Project File Line Suppression State Error (active) E0147 declaration is incompatible with "int Player::getAge() const" (declared at line 21 of "c:\Users\Thomas\source\repos\Thomas' Game\Thomas' Game\Player.h") Thomas' Game c:\Users\Thomas\source\repos\Thomas' Game\Thomas' Game\Player.cpp 15 – Tomhass Jan 13 '18 at 23:36
  • These lines `cin >> player.setName; cout << "Hello " << player.getName << endl` are wrong. – Ed Heal Jan 13 '18 at 23:38
  • You can't stream from `cin` directly to the argument of a function. You would need to get the user's input into a `std::string` and then pass that on to `setName`. – François Andrieux Jan 13 '18 at 23:45
  • It looks like you're trying to code Java in C++ and then hoping that C++ features like `cin` play nice. If you want to use C++ features to their fullest extent, you need to code the way C++ intended. – Silvio Mayolo Jan 13 '18 at 23:55

2 Answers2

3

First error means that you can't pass input directly from cin to setName function, you should first store it in a variable like this:

string name;
cin >> name;
player.setName(name);

Second error is there just because you are missing parenthesis on getName:

cout << "Hello " << player.getName() << endl;
Becks
  • 468
  • 1
  • 5
  • 12
1

Player::setName is a function. You probably want something like this.

string name;
cin >> name;
player.setName(name);

This reads the next string (white-space separated) from the standard input into a string and then calls your setter with that string.

Here's a reference for the std::istream::operator>>