我正在阅读一本关于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