Book.h:
#ifndef BOOK_H
#define BOOK_H
#include <string>
class Author;
class Book {
public:
Book(const std::string title, const std::string publisher, const Author& author, int isbn, double price);
std::string getTitle() const;
Author getAuthor() const;
Book& setAuthor(const Author& author);
private:
std::string b_title;
std::string b_publisher;
Author b_author;
int b_isbn;
double b_price;
};
Author.h:
#include <string>
#ifndef AUTHOR_H
#define AUTHOR_H
class Book;
class Author {
friend Book::getAuthor() const;
friend Book::setAuthor(const Author& author);
public:
Author(const std::string& name, const std::string& email, Gender gender);
Author& setName(const std::string& name);
private:
std::string a_name;
std::string a_email;
int a_gender;
};
#endif
在author.h中的错误,在友元函数上:
friend Author Book::getAuthor() const;
错误是:
error: invalid use of incomplete type 'class Book'
error: forward declaration of 'class Book'
当然,你说这是一个重复的问题。我看到了你所写的内容,但我担心它没有涵盖错误。