比较c ++字符串

Chr*_*ine 3 c++ string compare

class Song {

public:
    const string getAutherName();
}

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

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

}
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

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]
Run Code Online (Sandbox Code Playgroud)

它为什么使用basic_string而不是string!怎么解决这个问题?

Rei*_*ica 5

你的getAutherName()功能不是const,所以不能通过a来调用const Song&.像这样更改函数声明:

class Song {

public:
    const string getAutherName() const;
}
Run Code Online (Sandbox Code Playgroud)