Reputation: 573
I'm trying to send an Album object from main to a function in another .cpp file but I'm getting errors when compiling:
From main I create an Album object and then try to pass it to the menu function like so:
Model::Album album("TestAlbum");
View::Menu m;
m.startMenu(album);
My menu class:
#include "stdio.h"
#include "Menu.hpp"
#include "AlbumOps.hpp"
#include "Album.hpp"
#include <iostream>
using namespace std;
namespace View
{
void Menu::startMenu(Model::Album inAlbum) //compile errors happen here
{
int option = 1;
while (option!=5)
{
cout << "1. Add image to album\n";
cout << "2. Remove image from album\n";
cout << "3. List all images in album\n";
cout << "4. View image in album\n";
cout << "5. Quit\n";
//and so on
When I try to compile this, I get errors on the void Menu::startMenu(Model::Album inAlbum) line
'Model' has not been declared
Model is a namespace I use. I thought including the Album.hpp would fix this but it hasn't and I'm at a loss for how to fix this.
Edit: Menu is a class, here is my Menu.hpp:
#ifndef MENU_H //"Header guard"
#define MENU_H
namespace View
{
class Menu
{
public:
void startMenu(Model::Album inAlbum);
};
}
#endif
And my Album.hpp:
#ifndef ALBUM_H
#define ALBUM_H
#include <string>
#include <vector>
#include "Image.hpp"
namespace Model{
class Album
{
private:
std::vector<Image*> imageList;
std::string albumName;
public:
Album(std::string);
/****Setters****/
void setAlbumName(std::string);
void addImage(Image);
/****Getters****/
Image getImage(int);
std::string getAlbumName();
int getListLength();
};
}
#endif
Upvotes: 0
Views: 155
Reputation: 24626
Your Menu.hpp
is missing some declarations. Headers should be compilable on their own, without the need to include other headers before them. So if you are not sure you should always try to compile something like this:
#include "Menu.hpp"
int main() {
}
If it does not compile, you need to add includes or declarations in the header.
Now what is missing in your Menu.hpp is a declaration of the Album class. Just including the album header would be overkill and lead to circular includes, so a forward declaration is the right thing to do:
#ifndef MENU_H //"Header guard"
#define MENU_H
//forward declaration of Album:
namespace Model {
class Album;
}
namespace View
{
class Menu
{
public:
void startMenu(Model::Album inAlbum);
};
}
#endif
In you Album header, the include of the Image header is too much. A forward declaration would suffice, since you don't actually use an Image, you only use pointers to Image and declare its type as return and parameter type of some functions. You might need to include the image header in your Album.cpp though.
For more information about includes and forward declarations read this excellent GOTW article from Herb Sutter.
Upvotes: 1