Reputation: 13
I have a question about building a method :
virtual std::string getPerson() const;
I have a child class Player and a the parent is Person.
Class Player :
class Player : public Person {
public:
Player(const std::string& p_name,const std::string& p_lastname, const int& p_age, const std::string& p_position);
virtual ~Player();
virtual Person* clone() const;
std::string getPosition() const;
virtual std::string getPerson() const;
private:
std::string m_position;
};
Class Person :
class Person {
public:
Person(const std::string& p_name,const std::string& p_lastname, const int& p_age);
virtual ~Person();
virtual std::string getPerson() const;
std::string getName() const;
std::string getLastName() const;
int getAge() const;
private:
std::string m_name;
std::string m_lastname;
int m_age;
};
When I try to add this in Player :
std::string Player::getPerson()
{
ostringstream os;
os << "Name :" << getName() << "\n";
os << "LastName :" << getLastName()() << "\n";
os << "Age :" << getAge()() << "\n";
os << "Position :" << getPosition();
return os.str();
}
I get Member declaration not found
I can't get it to work I would need to print something like this :
Name : John
Lastname : Smith
Age : 22
Position : Goalie
Upvotes: 1
Views: 63
Reputation: 25663
Simple thing:
std::string getPerson() const;
is not the same as:
std::string getPerson();
If you can use c++11 use the override keyword, that protects you from such kind of mistakes. In your case the compiler detects the problem. But if you have other variants you maybe declare a new method instead of overloading!
Upvotes: 0
Reputation: 27538
You missed the const at the end of the function signature. This should work:
std::string Player::getPerson() const
{
ostringstream os;
os << "Name :" << getName() << "\n";
os << "LastName :" << getLastName()() << "\n";
os << "Age :" << getAge()() << "\n";
os << "Position :" << getPosition();
return os.str();
}
But please mind what I said in the comment and change the function's name, or even better, make your class work with std::ostream
by means of overloading operator<<
.
Upvotes: 1