3

I have an Album class in "album.h" like this:

#include "song.h"

class Album
{
public:
Album(string _id, string _title, string _singer, float _price, vector<Song> _songs) : id(_id), title(_title), singer(_singer), price(_price), songs(_songs), availableAlbums(10) {}
void add_song(Song s){ songs.push_back(s); }
void add_availableAlbums(int added){ availableAlbums += added; }
string get_id(){ return id; }
string get_singer(){return singer;}

private:
string id;
string title;
string singer;
float price;
vector <Song> songs;
int availableAlbums;
};

and a Song class in "song.h" like this:

#include "album.h"

class Song
{
public:
Song(string _numOfSong, string _title, string _singer, string _duration, float _price): 
numOfSong(_numOfSong), title(_title), singer(_singer), duration(_duration), price(_price){}

private:
string numOfSong;
string title;
string singer;
string duration;
float price;
Album* album;
};

we know that every Song has an Album ( every Song must point to its Album) and i do that by initialize one Album* for every Song and i have some errors that are here:

error C2061: syntax error : identifier 'Album'
error C2065: '_album' : undeclared identifier
error C2143: syntax error : missing ';' before '*'
error C2614: 'Song' : illegal member initialization: 'album' is not a base or member
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

thanks

5
  • I am not sure I see what the problem is. Try this. Although I would use a smart pointer for this. Commented Dec 24, 2014 at 13:57
  • 2
    "how can I make every Song to point to its Album?" Depends on how you're instatiating your Song instances in class Album, most probably you pass a this pointer there. Commented Dec 24, 2014 at 14:00
  • 1
    Forward declare Album in Song.h before your class declaration. class Album; Commented Dec 24, 2014 at 18:35
  • @mama23n Now (with the updated code) you have a circular include. Also you are not using include guards. Commented Dec 24, 2014 at 19:25
  • 1
    stackoverflow.com/questions/625799/… Commented Dec 24, 2014 at 19:27

2 Answers 2

9

Your Song class has an constructor that takes a pointer to the Album class so assume that you have the following code:

Album* album = new Album();
Song song = new Song(album);

In the first line you create a new album and in the second line you create a new song with the recently created album.

Album* album1 = song->album; // This is how you can access song's album

But, be aware that whenever you use new to create an object you should use delete keyword when you finished working with that object to free the memory used for that object.

So, there is no problem in your code or at least I can't find any.

Sign up to request clarification or add additional context in comments.

4 Comments

Nice effort, but I would not ever recommend using new and delete at all, but shared_ptr and make_shared or their unique equivalents instead.
yes. you are right but I'm asking for how to pass album to constructor because fourth line of Song class have error and I don't know what is that for.
@mama23n We can't help you if you do not provide enough information, tell us what error do you get.
@Shahriyar I do that
1

The code is correct, you just have to make sure that the Album instance exists during the lifetime of the Song instances. If the Song::album pointer does not get changed, it might be more clean to use a reference instead. This means that album cannot be made to point to another album after construction.

class Song {
public:
    Song(Album& _album) : album(_album) { }
private:
    Album& album;
}

But then the Song cannot be assigned by the default operator=. It would need to be overloaded to check if the other Song's album is the same, and only assign the other attributes.

If the Song does not modify the Album object, const-correctness can be enforced by using instead const Album* (or const Album&) both as the member variable type and construction argument.

If the lifetime of the Album object is not manually controlled, you could use std::shared_ptr<Album>.

4 Comments

but it has error in fourth line of Song class (in Song constructor), and I don't know what to do...
@mama23n What error? Please edit your question to include the exact error you get along with some context. With the information that you have given we have to play a guess the problem game..
@drescherjm I do that
fixed the error in this reply

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.