我有一个在 "album.h" 中的 Album
类,代码如下:
#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;
};
还有一个在 "song.h" 文件中的 Song
类,如下所示:
#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;
};
我们知道每首歌都有一个专辑(每首歌必须指向它的专辑),我通过为每首歌初始化一个专辑*来实现这一点,但我遇到了一些错误,这些错误在此处:
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
class Album;
进行Album的前向声明。 - drescherjm