0

I am currently learning about classes and am having a problem in my class implementation file.

In my class header/specification file Book.h , I have the public member function setPages.

#ifndef BOOK_H
#define BOOK_H
#include <string>
#include "Author.h"
#include "Publisher.h"

class Book
{
private:
    std::string _title;
    std::string _edition;
    int _pages;
    int _copyrightYear;
    Author _author;
    Publisher _publisher;
public:
    Book (std::string title, Author author, Publisher publisher)
    {_title = title; _author = author; _publisher = publisher;}

    void setPages (int pages);
    void setCopyYear (int copyrightYear);
    void setEdition (std::string edition);
    std::string getTitle () const;
    std::string getEditon () const;
    int getPages () const;
    int getCopyYear () const;
    Author getAuthor () const;
    Publisher getPublisher () const;

};
#endif

In my Book.cpp implementation file I have

#include <string>
#include "Author.h"
#include "Publisher.h"
#include "Book.h"

void Book::setPages (int pages)
{
    _pages = pages;
}

I keep getting the error that Book is not a classname or namespace but I don't see what I've done wrong. I included my Book header file and checked to make sure everything was spelled correctly in the class. I've done the same thing in my other classes and it is working so I don't see why this isn't.

Any help appreciated thanks.

Here is Publisher.h and Author.h

#ifndef PUBLISHER_H
#define PUBLISHER_H


class Publisher
{
private:
    std::string _name;
    std::string _address;
    std::string _phoneNumber;
public:
    Publisher (std::string& name)
    {_name=name;}

   void setAddress (std::string address);
   void setNumber (std::string phoneNumber);

    std::string getAddress () const;
    std::string getNumber () const;

    bool operator==(std::string name)
    {
        if (_name == name)
            return true;
        else 
            return false;
};

#endif

and Author.H

#ifndef AUTHOR_H
#define AUTHOR_H

class Author
{
private:
    std::string _name;
    int _numberOfBooks;
public:
    Author(std::string& name)
    {_name = name;}

    void setNumOfBooks (int numberOfBooks);
    int getNoOfBooks () const;

    bool operator==(std::string _name)
    {
        if (this->_name == _name)
            return true;
        else
            return false;
    }
};
#endif
sircrisp
  • 947
  • 4
  • 16
  • 30
  • 3
    What files do `Author.h` and `Publisher.h` include? If either includes `book.h`, you have a circular dependency in the includes. – Mooing Duck Mar 27 '12 at 17:05
  • 1
    Also: `Book (const std::string& title, const Author& author, const Publisher& publisher) : _title(title), _author(author), _publisher(publisher) {}` – Mooing Duck Mar 27 '12 at 17:07
  • Neither of them contain any files they are just specification files for `Author.cpp` and `Publisher.cpp`, which include implementation instructions like `Book.cpp` – sircrisp Mar 27 '12 at 17:07
  • 1
    try replacing `#include "Author.h"` and `#include "Publisher.h"` with `class Author;` and `class Publisher;` in you Booh.h file. – andre Mar 27 '12 at 17:09
  • 1
    Or, in C++11, `Book(std::string title, Author author, Publisher publisher) : _title(std::move(title)), _author(std::move(author)), _publisher(std::move(publisher)) {}`. Note that every parameter is passed by value!! – Nawaz Mar 27 '12 at 17:09
  • http://stackoverflow.com/questions/4816698/avoiding-circular-dependencies-of-header-files – Mooing Duck Mar 27 '12 at 17:10
  • I've done everything suggested but the error still occurs. – sircrisp Mar 27 '12 at 17:11
  • @sircrisp: I somehow doubt that your `Author.h` and `Publisher.h` don't include any files. Please show their includes if they have them. – Mooing Duck Mar 27 '12 at 17:11
  • I've just included `Author.h` and `Publisher.h`. – sircrisp Mar 27 '12 at 17:17
  • 5
    `bool operator==(std::string name)` in "Publisher.h" is missing a brace in your example. Is that actually in your code or a copy and paste error? – andre Mar 27 '12 at 17:29
  • 1
    That was in my code.. I don't know why but it corrected my problem. :p – sircrisp Mar 27 '12 at 17:30
  • makes perfect sense if you look at how the c++ pre-compilers work when you use the #include macro. Happy to be of help! – andre Mar 27 '12 at 17:35
  • 1
    @ahenderson, you need to turn that into an answer so you can get some credit. – Mark Ransom Mar 27 '12 at 17:58
  • @sircrisp: Those other files should include `string`. I should be able to compile (but not link) each header file by itself. – Mooing Duck Mar 27 '12 at 17:59

2 Answers2

0

Until @ahenderson decides to turn his comments into an answer:

bool operator==(std::string name) in "Publisher.h" is missing a brace in your example. is that actually in your code or a copy and paste error?

bool operator==(std::string name)
{
    if (_name == name)
        return true;
    else 
        return false;

Oops, no brace here!

Bo Persson
  • 86,087
  • 31
  • 138
  • 198
0

Suggestion: Simplify your operator== method:

The expression _name == name will already return true or false. No need to put it into an if clause that returns true or false.

Try this:

bool operator==(const std::string& name)
{
    return (_name == name);
}

In the above example, the expression is evaluated and the result is returned, directly.

Also, you may run into compiler issues if your variables begin with an underscore, '_'. Change your naming convention so this issue doesn't raise its ugly head. Two common practices are to append a suffix, name_, or prefixing with something like m_name.

Thomas Matthews
  • 52,985
  • 12
  • 85
  • 144