Pass std::string to constructor

D

darrell.blake

I'm trying to pass an std::string to an object's constructor but I'm
getting errors. This is my code:

cout << "Enter name for Player 1: ";
string name;
getline(cin, name);
Player1 player1(name);

I get an error on the last line stating "error C2440: 'initializing' :
cannot convert from 'std::string' to 'Player'" It says that no
constructor could take the source type, or constructor overload
resolution was ambiguous.

This the constructor for my Player class:

class Player {
public:
Player(string name);
....
private:
string name;
};

Player::player(string name) {
this->name = name;
}

I can't see what I'm doing wrong. I'm making the jump to using
std::string after using char* for years.
 
R

Rolf Magnus

I'm trying to pass an std::string to an object's constructor but I'm
getting errors. This is my code:

cout << "Enter name for Player 1: ";
string name;
getline(cin, name);
Player1 player1(name);

I get an error on the last line stating "error C2440: 'initializing' :
cannot convert from 'std::string' to 'Player'"

That's strange, considering you are actually requesting a conversion from
string (std::string?) to Player1. Are you sure this is the code you
actually tried?
It says that no constructor could take the source type, or constructor
overload resolution was ambiguous.

This the constructor for my Player class:

class Player {
public:
Player(string name);
...
private:
string name;
};

Player::player(string name) {
this->name = name;
}

I can't see what I'm doing wrong.

Well, I can see errors in the code, but none that fit to the error message.
Post a minimal, but complete program that you have actually given to your
compiler and that contains the error.
 
D

darrell.blake

Here's the code. To be fair, I've only just started writing the
program.

main.cpp

#include <iostream>
#include "Player.h"

using namespace std;

int main() {
cout << "Enter Name for Player 1: ";
string name;
getline(cin, name);
Player player1(name);
cout << "Enter Name for Player 2: ";
getline(cin, name);
Player player2(name);
cout << "\nCurrent players are " << player1.getName() << " and " <<
player2.getName() << endl;

cin >> name;
return 0;
}


Player.h

#include <string>

class Player {
public:
Player(string name);
string getName();
private:
string name;
};


Player.cpp

#include "Player.h"

Player::player(string name) {
this->name = name;
}

string Player::getName() {
return name;
}


I write my own games and have been doing for a while but this is the
first game where I've considered using the standard library.
 
R

Rolf Magnus

Here's the code. To be fair, I've only just started writing the
program.

You don't qualify string, which is in namespace std. When change every
occurance of 'string' into 'std::string', it compiles.
main.cpp

#include <iostream>
#include "Player.h"

using namespace std;

int main() {
cout << "Enter Name for Player 1: ";
string name;
getline(cin, name);
Player player1(name);
cout << "Enter Name for Player 2: ";
getline(cin, name);
Player player2(name);
cout << "\nCurrent players are " << player1.getName() << " and " <<
player2.getName() << endl;

cin >> name;
return 0;
}


Player.h

You should add include guards.
#include <string>

class Player {
public:
Player(string name);
string getName();
private:
string name;
};


Player.cpp

#include "Player.h"

Player::player(string name) {
this->name = name;
}

Some style issue not related to the error you're getting: Pass by reference
to reduce unnecessary copies, and use initializer lists, like:

Player::player(const std::string& name)
: name(name)
{
}
 
J

Jonathan Mcdougall

Here's the code. To be fair, I've only just started writing the
program.

main.cpp

#include <iostream>
#include "Player.h"

Don't rely on other headers to include headers you need. Include
using namespace std;
http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5

int main() {
cout << "Enter Name for Player 1: ";
string name;
getline(cin, name);
Player player1(name);
cout << "Enter Name for Player 2: ";
getline(cin, name);
Player player2(name);
cout << "\nCurrent players are " << player1.getName() << " and " <<
player2.getName() << endl;

cin >> name;

[ot]
If that's for keeping the console open, just cin.get() does the job
also
[/ot]
return 0;
}


Player.h

#include <string>

Here, you don't have a namespace directive so "string" does not exist,
only "std::string" does. Solution: since this is a header, don't do
"using namespace std" (see the FAQ), just fully qualify the names.

The compiler message is weird, but it probably meant that it cannot
convert an std::string (from main()) to a "string" (in player.h)
because it doesn't know what "string" is.
class Player {
public:
Player(string name);

Player(const string& name);
string getName();

string getName() const;
private:
string name;
};


Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top