2015-07-13 84 views
0

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

你说你尝试过'Biblo :: Book :: Horror' - 我也是这样,它的工作原理(clang ++ 3.6 ),所以发布你的工具链信息,并从你的帖子中删除所有不相关的垃圾,但*仍*会产生你的问题。 [看它活着](http://ideone.com/0s6LY8)。 – WhozCraig

+0

我尝试了你在评论中的链接,它也适用于我的结局。不完全确定你的工具链是什么意思,但我收到的错误如下: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' –

+0

该错误与您的枚举的可用性完全无关。 – WhozCraig

回答

1

您需要包括枚举类。例如:

Biblio::Book::Genre::Horror

+1

这是真的,如果这是一个枚举类,但它不是,它只是一个枚举。 –

3

看来你什么都试过了,但你需要的东西! enum嵌套在位于Biblo名称空间内的Book类中。您正在查找的代码是:

int main() 
{ 
    Biblo::Book book(100, 2012, "The Walrus", "The Eggman", Biblo::Book::Horror); 
    return 0; 
} 
+0

使用此产生一个未定义的参考错误 –

+0

根据您的问题中的评论来看,您的新问题与您的'enum'的可用性无关(谢谢@WhozCraig的单词并且最初指出了这一点)。你的问题是关于访问你的'枚举',这回答。如果您还有其他问题,我建议您提出一个新问题,不要让这个问题困扰(道歉,如果这听起来很苛刻,我的意思是最好的方式。如果您发布新问题,提问更多也没有错) – Tas

0

一堆事情真的会出错。 正如其他人所说,你的枚举被隐藏在比你想象的更深的一层,而你关于修复它的投诉然后产生一个未定义的参考可能是因为在那时你遇到了事实没有什么初始化的事实,如果你通过了当涉及到统计员时,您返回的项目有点不好。

如果你使用了正确的命名空间,迅速把在实际实施的构造,并获得您的枚举最直接的回报(一个int可能工作)它应该工作,而且很可能是这样的:

#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; } 
    int getGenre() const { return genre; } 

    // Mutators 
    void changeAuthor(string newAuthor); 
private: 
    int ISBN; 
    int copyrightYear; 
    string title; 
    string author; 
    Genre genre; 

}; // End Book 

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; 
}; 
} 
using namespace std; 

int main() 
{ 
    Biblo::Book book(100, 2012, "The Walrus", "The Eggman", Biblo::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; 
} 

你是否起诉你正在关注的书不是讨论细节,如初始化程序列表或其他与构造函数并发或与之前的主题相关的其他内容?

代码看起来有些不完整。 在这里编辑在SO上,所以忍着可怜的格式和合并的h/cpp看看:)

+0

也,您的getgenre完全未实现,该枚举的实际值也是如此。在学习时,我建议你拍摄简单但至少有点完整的对象和属性的实现。 –

+0

我在一个名为book.cpp的文件中有一个实现,其中包含book.h,我的构造函数/非内联函数在那里。我会更新主帖以显示book.cpp –

+0

当我寻求帮助时,我发现如果你可以把东西放在一起,以便有人可以将它们粘贴到像ideone这样的在线服务中,你就更有可能获得帮助。可以粘贴和更正的简化但完整的实现可帮助其他人帮助您。因人而异 –