我試圖從主發送相冊對象給一個函數在其他.cpp文件,但在編譯時遇到錯誤:頭文件和命名空間的混亂
從主我創建一個相冊對象,然後嘗試將它傳遞給菜單功能,像這樣:
Model::Album album("TestAlbum");
View::Menu m;
m.startMenu(album);
我的菜單類:
#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
當我嘗試編譯,我得到就作廢了菜單的錯誤:: STARTMENU(型號::相冊inAlbum)行
'Model'尚未聲明
Model是我使用的命名空間。我認爲包括Album.hpp會解決這個問題,但它沒有,我不知道如何解決這個問題。
編輯:菜單是一類,這是我Menu.hpp:
#ifndef MENU_H //"Header guard"
#define MENU_H
namespace View
{
class Menu
{
public:
void startMenu(Model::Album inAlbum);
};
}
#endif
而且我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
什麼是「菜單」?它在哪裏宣佈?它是如何聲明的? – zentrunix
問題很可能出現在「Album.hpp」或其他標題之一中。 –
把#include「Album.hpp」放在上面#include「Menu.hpp」 – JackyZhu