2015-07-13 100 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這樣的在線服務中,你就更有可能獲得幫助。可以粘貼和更正的簡化但完整的實現可幫助其他人幫助您。因人而異 –