C++ Destructors

I can’t figure out the last part of the destructors lesson. When the destructor is called I am supposed to output "Goodbye " + The name of the object + “!”. COde academy says that I am successful in the lesson though when I try to compile the code I get the error “expected primary-expression before ‘.’ token” from the following code.

Song::~Song() {
std::cout << "Goodbye " << Song.get_title() << “!”;
}
// Song::Song.get_title(), Song::back_to_black.get_title(), and back_to_black.get_title() don’t work.

I know in Python Self notation would be used, but I am not sure how that translates into C++. How do I properly refer to an object in C++.

The full code is below.
// music.cpp
#include
#include “song.hpp”

int main() {
Song back_to_black(“Back to Black”, “Amy Winehouse”);
}

//song.hpp
#include

class Song {

std::string title;
std::string artist;

public:
Song(std::string new_title, std::string new_artist);
~Song();
std::string get_title();
std::string get_artist();
};

//song.cpp
#include “song.hpp”
#include

Song::Song(std::string new_title, std::string new_artist)
: title(new_title), artist(new_artist) {}

// add the Song destructor here:
Song::~Song() {
std::cout << "Goodbye " << Song.get_title() << “!”;
}

std::string Song::get_title() {
return title;
}

std::string Song::get_artist() {
return artist;
}

I also initially tried to call .get_song but couldn’t make it work, maybe someone who knows better can explain why, does it have something to do with the destructor being called first?

I used:

std::cout << "Goodbye: " << title;

This worked, but I don’t know if it’s best practice.

Song::~Song() {
std::cout << "Goodbye "<< get_title();
}

this is how lol yaaaay i was just checking ur code and i tried it u can’t call the class again its a function u want to call :wink:

1 Like