0

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

the error in author.h, on the friend function:

friend Author Book::getAuthor() const;

the error is:

error: invalid use of incomplete type 'class Book'
error: forward declaration of 'class Book'

previusly, you said it is a duplicate questions. I saw what you've written befor about that, but i'm afraid it didn't cover the error.

Yair Marom
  • 33
  • 7
  • Why you need declare the member function of class `Book` as the friend of `Author`? Without that, you even need not include `book.h` in `author.h`. – songyuanyao Apr 04 '15 at 15:04
  • becouse i used that: Author Book::getAuthor() const{ return b_author; } Book& Book::setAuthor(const Author& author){ b_author.Author(author); return *this; } – Yair Marom Apr 04 '15 at 17:23
  • Did you use any `private` member of `Author` in these two methods? Accessing the `private` or `protected` part of a class is the only reason to declare a friend. – songyuanyao Apr 05 '15 at 03:58

0 Answers0