我正在閱讀一本關於C++的書,並且寫出了一些使用類的接口和實現來練習的代碼。我一直在尋找解決方案,我的問題一段時間無濟於事。使用類的枚舉(C++)
我有一個枚舉類的內部。在嘗試實例化該類的對象時,我無法訪問該類之外的枚舉類型。我嘗試過使用Book :: Horror,Biblo :: Horror,Biblo :: Book :: Horror,Horror,甚至像Biblo :: Book :: Genre :: Horror。似乎無法讓它訪問main.cpp文件中實例化對象的枚舉類型。
任何幫助表示讚賞! C++的更復雜的用途對我來說仍然是新的。下面是我的源:
book.h
#include <iostream>
#include <string>
using namespace std;
namespace Biblo{
class Book{
public:
enum Genre{
No_Genre, Horror, Comedy, Romance, Mystery
};
// The rest of this header is working fine I think, just this enum
class Invalid{}; // Used for throwing errors
Book(int n_ISBN, int n_copyrightYear, string n_title, string n_author, Genre n_genre);
Book();
// Accessors (non-modifying)
int getISBN() const { return ISBN; }
int getCopyrightYear() const { return copyrightYear; }
string getTitle() const { return title; }
string getAuthor() const { return author; }
string getGenre() const;
// Mutators
void changeAuthor(string newAuthor);
private:
int ISBN;
int copyrightYear;
string title;
string author;
Genre genre;
}; // End Book
// Helper Functions go here
bool operator==(const Book& a, const Book& b);
bool operator!=(const Book& a, const Book& b);
} // End Biblo
和main.cpp中
#include <iostream>
#include "book.h"
using namespace std;
int main()
{
Biblo::Book book(100, 2012, "The Walrus", "The Eggman", Book::Horror); // THIS LINE GIVES ERROR
cout << "ISBN: " << book.getISBN() << endl;
cout << "Copyright: " << book.getCopyrightYear() << endl;
cout << "Title: " << book.getTitle() << endl;
cout << "Author: " << book.getAuthor() << endl;
cout << "Genre: " << book.getGenre() << endl;
return 0;
}
編輯:這裏是book.cpp文件
#include <iostream>
#include "book.h"
#include <string>
namespace Biblo{
Book::Book(int n_ISBN, int n_copyrightYear, string n_title, string n_author, Genre n_genre)
:ISBN(n_ISBN), copyrightYear(n_copyrightYear), title(n_title), author(n_author), genre(n_genre)
{
// constructor
}
Book::Book()
:ISBN(0), copyrightYear(0), title(""), author(""), genre(Genre::No_Genre)
{
// Default constructor
}
// Accessors
string Book::getGenre() const
{
if (Book.genre == Genre::No_Genre)
return "No Genre!";
if (Book.genre == Genre::Horror)
return "Horror";
if (Book.genre == Genre::Comedy)
return "Comedy";
if (Book.genre == Genre::Romance)
return "Romance";
if (Book.genre == Genre::Mystery)
return "Mystery";
}
// Mutators
void Book::changeAuthor(string newAuthor)
{
author = newAuthor;
}
// Helper Functions
bool operator==(const Book& a, const Book& b)
{
if (a.getISBN() != b.getISBN())
return false;
if (a.getCopyrightYear() != b.getCopyrightYear())
return false;
if (a.getTitle() != b.getTitle())
return false;
if (a.getAuthor() != b.getAuthor())
return false;
if (a.getGenre() != b.getGenre())
return false;
return true;
}
bool operator!=(const Book& a, const Book& b)
{
return !(a==b);
}
} // End Biblo
你說你嘗試過'Biblo :: Book :: Horror' - 我也是這樣,它的工作原理(clang ++ 3.6 ),所以發佈你的工具鏈信息,並從你的帖子中刪除所有不相關的垃圾,但*仍*會產生你的問題。 [看它活着](http://ideone.com/0s6LY8)。 – WhozCraig
我嘗試了你在評論中的鏈接,它也適用於我的結局。不完全確定你的工具鏈是什麼意思,但我收到的錯誤如下:C:/ Users/Student/Dropbox/C++/Book Class/main.cpp:8:未定義的引用Biblo :: Book :: Book int,int,std :: string,std :: string,Biblo :: Book :: Genre)' C:/ Users/Student/Dropbox/C++/Book Class/main.cpp:14:undefined reference to'Biblo: :Book :: getGenre()const' –
該錯誤與您的枚舉的可用性完全無關。 – WhozCraig