Christine
Christine

Reputation: 113

Copy Class Constructors C++

i have a class Song that i want to copy...

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

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

I get this errors:

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

Upvotes: 1

Views: 124

Answers (2)

Philipp Claßen
Philipp Claßen

Reputation: 43970

Try this:

Song copied_song(*this);

The copy constructor is defined as Song(const Song&), but this is a pointer to Song. Therefore, you need to dereference it.

The line below is somewhat confusing to me:

this->Song(copied_song);

I guess it was only another try to call the copy constructor, wasn't it? Anyway, it doesn't work that way. Use either the solution at the top of my answer, or use:

Song copied_song = *this;

Upvotes: 2

Kerrek SB
Kerrek SB

Reputation: 476950

Say Song copied_song(*this);.

Remember that this is a pointer, but the copy constructor takes a reference.

Upvotes: 5

Related Questions