gpt4 book ai didi

c++ - 使用类中的枚举 (C++)

转载 作者:行者123 更新时间:2023-11-27 22:57:25 25 4
gpt4 key购买 nike

我正在阅读一本关于 C++ 的书,并且正在编写一些代码来练习使用类的接口(interface)和实现。我已经为我的问题搜索了一段时间的解决方案,但无济于事。

我有一个包含枚举的类。在尝试实例化该类的对象时,我无法访问类外的枚举类型。我尝试过使用 Book::Horror、Biblo::Horror、Biblo::Book::Horror、Horror,甚至 Biblo::Book::Genre::Horror。似乎无法让它让我访问枚举类型以在 main.cpp 文件中实例化我的对象。

感谢任何帮助! C++ 更复杂的用法对我来说仍然是新的。这是我的来源:

书.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

最佳答案

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

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

关于c++ - 使用类中的枚举 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31375316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com