1

我有一首班级歌曲,我想复制...

int mtm::Song::getLimitedLength(int maximum_length) {

        Song copied_song(this);
    this->Song(copied_song);
}

我得到这个错误:

Multiple markers at this line
- candidates are:
- no matching function for call to 'mtm::Song::Song(mtm::Song* const)'
4

2 回答 2

5

Song copied_song(*this);

请记住,这this是一个指针,但复制构造函数需要一个引用

于 2013-01-19T00:56:22.313 回答
2

尝试这个:

Song copied_song(*this);

复制构造函数定义为Song(const Song&),但this它是指向 Song 的指针。因此,您需要取消引用它。

下面的行让我有些困惑:

this->Song(copied_song);

我想这只是再次尝试调用复制构造函数,不是吗?无论如何,它不会那样工作。使用我答案顶部的解决方案,或使用:

Song copied_song = *this;
于 2013-01-19T00:56:36.323 回答