0

I'm reading a book on C++ and was writing out some code to practice using the interface and implementation of a class. I've searched for solutions to my issue for a while to no avail.

I have a class with an enumeration inside of it. While trying to instantiate an object of that class, I am having trouble accessing the enum types outside of the class. I have tried using Book::Horror, Biblo::Horror, Biblo::Book::Horror, Horror, and even things like Biblo::Book::Genre::Horror. Can't seem to get it to let me access the types of the enum for the instantiation of my object in the main.cpp file.

Any help is appreciated! The more complex uses of C++ are still new to me. Here is my source:

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

and 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;
}

Edit: here is the book.cpp file

#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
3
  • 1
    You say you tried Biblo::Book::Horror - so did I, and it works (clang++ 3.6), so post your toolchain info and strip all the unrelated junk out of your post , but still produces your problem. see it live. Commented Jul 13, 2015 at 4:18
  • I tried the link you had in your comment and it worked on my end as well. Not entirely sure what you mean by toolchain, but the error i recieve is as follows:C:/Users/Student/Dropbox/C++/Book Class/main.cpp:8: undefined reference to 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' Commented Jul 13, 2015 at 5:48
  • That error has absolutely nothing to do with the availability of your enum. Commented Jul 13, 2015 at 6:15

3 Answers 3

3

It seems you tried everything but the thing you needed! The enum is nested inside the Book class which is within the Biblo namespace. The code you are looking for is:

int main()
{
    Biblo::Book book(100, 2012, "The Walrus", "The Eggman", Biblo::Book::Horror);
    return 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Judging by the comments in your question it appears your new problem has nothing to do with the availability of your enum (thanks @WhozCraig for the words and originally pointing this out). Your question was about accessing your enum, which this answers. If you are having other problems I suggest asking a new question as to not plague this one (apologies if this sounds harsh, I mean it in the best possible way. There's nothing wrong with asking more questions, if you post new questions)
1

You need to include the enum class. eg.:

Biblio::Book::Genre::Horror

1 Comment

That would be true if this were an enum class, but it's not, it's just an enum.
0

Bunch of things that are going wrong really. As others mentioned your enum is stashed one level deeper than you think it is, and your complaint about fixing it then producing an undefined reference is probably because at that point you bump into the fact there's nothing much initialized, and if you got past that you are returning items somewhat poorly when it comes to the enumerator.

If you use the right name space, quickly put in an actual implementation for the constructor, and get the most immediate return for your enum (an int might work) it should work, and probably look like this:

#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;
}

Are you sue the book you're following isn't discussing details such as initializer lists or something else for constructors concurrent to, or previous to, the subjects you're looking at?

The code looked somewhat incomplete. Edited in line here on SO, so bear with the poor formatting and the merged h/cpp look :)

3 Comments

Also, your getgenre was completely unimplemented, as were the actual values of that enum. When learning I suggest you shoot for simple but at least somewhat complete implementations of the objects and attributes you deal with.
I have an implementation in a file called book.cpp that includes book.h my constructors/non-inline functions are there. I'll update the main post to show book.cpp
When asking for help I find if you can put things together so that someone can just paste them into an online service like ideone you are more likely to get help. Reduced but complete implementations that can be pasted and corrected help others helping you. YMMV

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.