我遇到了循环依赖的问题,并且无法解决这个问题。
这是我在编译时收到的错误......
g++ --std=c++11 -ggdb Albums.cpp -c -o albums.o
In file included from Albums.hpp:9:0,
from Albums.cpp:1:
Artist.hpp:34:20: error: ‘Albums’ has not been declared
void setAlbums(Albums *albums) { _albums = albums; }
^
Artist.hpp:35:5: error: ‘Albums’ does not name a type
Albums *albums() { return _albums; }
^
Artist.hpp:46:5: error: ‘Albums’ does not name a type
Albums *_albums;
^
Artist.hpp: In constructor ‘Artist::Artist()’:
Artist.hpp:15:63: error: class ‘Artist’ does not have any field named ‘_albums’
_primaryImage(nullptr), _secondaryImage(nullptr), _albums(new Albums()) {}
^
Artist.hpp:15:75: error: expected type-specifier before ‘Albums’
_primaryImage(nullptr), _secondaryImage(nullptr), _albums(new Albums()) {}
^
Artist.hpp: In member function ‘void Artist::addAlbum(Album*)’:
Artist.hpp:31:9: error: ‘_albums’ was not declared in this scope
_albums->addAlbum(album);
^
Artist.hpp: In member function ‘void Artist::setAlbums(int*)’:
Artist.hpp:34:38: error: ‘_albums’ was not declared in this scope
void setAlbums(Albums *albums) { _albums = albums; }
^
Albums.cpp: In member function ‘void Albums::setArtistForAlbums(Artists*)’:
Albums.cpp:46:32: error: invalid use of incomplete type ‘class Artists’
for (int j = 0; j < artists->numArtists(); j++) {
^
In file included from Albums.cpp:1:0:
Albums.hpp:12:7: note: forward declaration of ‘class Artists’
class Artists;
^
Albums.cpp:48:33: error: invalid use of incomplete type ‘class Artists’
Artist *artist = artists->listOfArtists()->at(i);
^
In file included from Albums.cpp:1:0:
Albums.hpp:12:7: note: forward declaration of ‘class Artists’
class Artists;
^
Albums.cpp:48:55: error: ‘i’ was not declared in this scope
Artist *artist = artists->listOfArtists()->at(i);
Artist.hpp
有一个专辑对象Albums *_albums;
艺术家无法使用转发声明,因为我需要访问某些相册功能_albums->addAlbum(album);
Albums.cpp
在这里可以看到setImagesForAlbums也需要这张专辑
void Albums::setImagesForAlbums(AlbumImages *albumImages) {
for (int i = 0; i < albumImages->numAlbumImages(); i++) {
Album * album = albumWithID(albumImages->listOfAlbumImages()->at(i)->albumID());
if(album) {
if(albumImages->listOfAlbumImages()->at(i)->type() == "primary") {
album->primaryImage() = albumImages->listOfAlbumImages()->at(i);
}
else if(albumImages->listOfAlbumImages()->at(i)->type() == "secondary") {
album->secondaryImage() = albumImages->listOfAlbumImages()->at(i);
}
}
}
}
Albums.hpp
#ifndef __ALBUMS_HPP
#define __ALBUMS_HPP
#include <iostream>
#include "JSONArray.hpp"
#include "AlbumImages.hpp"
#include "Artist.hpp"
#include "Album.hpp"
class Artists;
class Albums: public JSONArray
{
public:
Albums(): JSONArray() {}
~Albums();
int numAlbums() { return listOfAlbums()->size(); }
void addAlbum(Album *album);
Album *albumWithID(unsigned int aID); // Not tested
void loadAlbumsFromFile(std::string fileName); //
std::string htmlString(); // Not tested
JSONDataObject *jsonObjectNode() { return new Album(); }
void setArtistForAlbums(Artists *artists);
void setImagesForAlbums(AlbumImages *);
std::vector<Album *> *listOfAlbums() { return (std::vector<Album *> *) _listOfDataObjects; }
void runAsserts(); // used for checking the integrity of this class.
void printAlbums();
};
#endif
Artist.hpp
#ifndef __ARTIST_HPP
#define __ARTIST_HPP
#include "JSONDataObject.hpp"
#include "ArtistImage.hpp"
#include "Albums.hpp"
// #include "Album.hpp"
// class Albums;
class Artist: public JSONDataObject {
public:
Artist(): JSONDataObject(), _artistID(0), cachedName(false), cachedRealName(false),
cachedProfile(false), cachedNumImages(false), cachedArtistID(false),
_primaryImage(nullptr), _secondaryImage(nullptr), _albums(new Albums()) {}
~Artist();
std::string profile();
std::string artistName();
std::string realName();
std::string numImages(); // since it's a string in the JSON file
unsigned artistID();
void print();
std::string htmlString(); // Implement
// the following 4 function-prototypes are new to this project.
void addAlbum(Album *album) {
// std::cout << "this in addAlbum is " << _albums << std::endl;
_albums->addAlbum(album);
}
void setAlbums(Albums *albums) { _albums = albums; }
Albums *albums() { return _albums; }
ArtistImage *&primaryImage() { return _primaryImage; } // GOOD
ArtistImage *&secondaryImage() { return _secondaryImage; } // GOOD
private:
std::string _name, _realName, _profile, _numImages;
unsigned _artistID;
bool cachedName, cachedRealName, cachedProfile, cachedNumImages, cachedArtistID;
// the following 3 variables are new to this project.
ArtistImage *_primaryImage, *_secondaryImage;
Albums *_albums;
};
#endif
似乎Artist.hpp需要Albums.hpp和Albums.hpp需要Artist.hpp创建循环依赖。如何避免这种情况?