3
class Song {

public:
    const string getAutherName();
}

void mtm::RadioManager::addSong(const Song& song,const Song& song1) {

    if (song.getAutherName() == song1.getAutherName())

}

I get this error:

Invalid arguments ' Candidates are: std::basic_string<char,std::char_traits<char>,std::allocator<char>> getAutherName() ' - passing 'const mtm::Song' as 'this' argument of 'std::string mtm::Song::getAutherName()' discards qualifiers [- fpermissive]

Why is it using basic_string and not string! how to fix this?

11
  • 1
    std::basic_string<... just is the actual thing used in the background. The error is still about your usage of std::string. Commented Jan 18, 2013 at 14:49
  • 2
    @MarounMaroun - this is C++, == is overloaded and allows for string equality comparisons. Commented Jan 18, 2013 at 14:49
  • @MarounMaroun this is c++ not java Commented Jan 18, 2013 at 14:49
  • 1
    std::string is just a typedef for std::basic_string<char> Commented Jan 18, 2013 at 14:50
  • 1
    Please copy the exact error message, what you have there looks strange. Also getAutherName() isn't const, you can't call with those Song references AFAICT. Commented Jan 18, 2013 at 14:52

4 Answers 4

5

Your getAutherName() function is not const, so it cannot be called through a const Song&. Change the function declaration like this:

class Song {

public:
    const string getAutherName() const;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Also, it should probably be returning const string &.
@Steve Depends on whether the string is stored or computed. With C++11's move semantics, I'd be more inklined to return just string. Not const string, though.
I'd guess that it's returning a private member so the copy in the return can't be elided (I think) so a reference is likely better in this case. But yeah, just const string is probably wrong.
2

You are calling getAutherName() on const Songs so you need to make that method const:

const string getAutherName() const;

It isn't clear why you return a const string. Either return a string, or return a const reference to one:

const string& getAutherName() const;
string getAutherName() const;

Comments

1

std::string is a typedef for basic_string<char, std::char_traits<char>, std::allocator<char> >, the compiler is just expanding the typedef in the error message.

However, why the code is not working I don't know. You appear to have cut out part of your error message where the ' is.

1 Comment

Odd, but as others have said, the problem is the method not being declared as const.
0

You need to add a const qualification to getAutherName to allow it to be called on const Song objects:

//| -- Returns a `const` `string` (this is bad practice
//v    because it inhibits optimization)
const string getAutherName() const;
// Can be called on a `const`    ^
// `Song`. This is good practice |
// because it it allows `getAutherName()`
// to be used in more places, and it encourages
// const-correctness.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.