1

Header

class Player {
protected:
    int age;
    string name;

public:
    int getAge();
    string& getName() const;

Definition

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

I get the following error when using the getName() function:

error: qualifiers dropped in binding reference of type string to initializer of type const string

How do I fix it and make it work?

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
  • 1
    Possible duplicate of [Error: Qualifiers dropped in binding reference of type x to initializer of type y](https://stackoverflow.com/questions/30146562/error-qualifiers-dropped-in-binding-reference-of-type-x-to-initializer-of-type) – Gino Mempin Dec 11 '18 at 23:52
  • 1
    It may be a duplicate for it but there is no problem-solving answer in that question i could find. – Yiğit Akkaş Dec 12 '18 at 00:11

1 Answers1

1

In order for it to be enforceable as const the return type needs to be a const&

const string& Player::getName() const
{
    return name;
}
Summer
  • 259
  • 1
  • 7
  • 18