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

Løiten
  • 3,185
  • 4
  • 24
  • 36
mama23n
  • 135
  • 2
  • 3
  • 10

2 Answers2

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.

cigien
  • 57,834
  • 11
  • 73
  • 112
Shahriyar
  • 106
  • 2
  • 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. – John Dibling Dec 24 '14 at 15:01
  • 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 Dec 24 '14 at 16:51
  • 1
    @mama23n We can't help you if you do not provide enough information, tell us what error do you get. – Shahriyar Dec 24 '14 at 17:18
  • @Shahriyar I do that – mama23n Dec 24 '14 at 18:53
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>.

tmlen
  • 8,533
  • 5
  • 31
  • 84