mercredi 29 juin 2016

Weird Linker errors in C++

These are the errors I'm getting

I honestly have no idea what these really means. Does it mean I typed in a character that isn't UTF? Just a bit confused.

This is libraryDriver.cpp

#include <iostream>
#include "Library.h"
#include "Book.h"
using namespace std;
int main()
{
    using namespace CS20A;
    Library l;
    l.addBookToCollection( "J. K. Rowlings", "Harry Potter and the Deathly Hallows" );
    l.addBookToCollection( "Barack Obama", "The Audacity of Hope" );
    l.addBookToCollection( "Vera Brittain", "Testament of Youth" );
    // Print out the library and its book
    cout << l << endl;
    try {
        l.checkoutBook( "Barack Obama", "The Audacity of Hope" );
        l.checkinBook( "Barack Obama", "The Audacity of Hope" );
        l.checkoutBook( "Vera Brittain", "Testament of Youth" );
    } catch( BookNotFound bnf ) {
        cout << "An error occurred here for the reason:";
        cout << bnf.what() << endl;
    }
    // Print out the library and its book - notice the difference
    cout << l << endl;
    // Let's try to throw some errors...
    try {
        // can't checkout a book not in the Library
        l.checkoutBook( "Barry Manilow", "Sweet Life : Adventures On The Way To Paradise" );
        cout << "An error occurred here..." ;
    } catch( BookNotFound bnf ) {
        bnf.what();
    }
    try {
        // can't checkout a book that is already being reserved
        l.checkoutBook( "Vera Brittain", "Testament of Youth" );
        cout << "An error occurred here..." ;
    } catch( BookNotFound bnf ) {
        bnf.what();
    }
}

Library.cpp

Thank you so so much. I went ahead and added things I believe where correct.

But I still get 2 more errors.

From what you've told me, I THINK that what the #3 error is saying is that inside Library.cpp, I'm referencing something in Book.h and so I have to include Book.h. I went ahead and did that but still got the error.

This is what my Library.cpp is now

#include "Library.h"
#include "Book.h"
#include <stdexcept>

using namespace std;
namespace CS20A{
    Library::Library(){
    }
    void Library::addBookToCollection(string author, string title){
        Book book(author,title);    
        myBooks[myNumberOfBooksSeenSoFar] = book;
        myNumberOfBooksSeenSoFar++;
    }
    void Library::checkoutBook(std:: string author, std:: string title) throw(...)
    {
        Book book(author, title);
        int booklocation;
        bool bookfound = false;

        for(int i = 0; i<myNumberOfBooksSeenSoFar; i++)
        {
            if(myBooks[i].getTitle() == book.getTitle() && myBooks[i].getAuthor() == book.getAuthor())
            {
                bookfound = true;
                booklocation = 1;
            }
        }
        if(!bookfound)
        {
            myBooks[booklocation].increaseNumberOfTimesCheckedOut();
        }
        else
        {
            throw BookNotFound();
        }
    }

    void Library::checkinBook(std::string author, std::string title) throw(...)
    {
        Book book(author, title);
        int booklocation;
        bool bookfound = false;

        for(int i = 0; i<myNumberOfBooksSeenSoFar; i++)
        {
            if(myBooks[i].getTitle() == book.getTitle() && myBooks[i].getAuthor() == book.getAuthor())
            {
                bookfound = true;
                booklocation = 1;
            }
        }
        if(bookfound)
        {

        }
        else
        {
            throw BookNotFound();
        }
    }
    ostream &operator<< (ostream& out, const Library & l)
    {
        for(int i = 0; i<l.myNumberOfBooksSeenSoFar; i++)
        {
            out << "Title: " << l.myBooks[i].getTitle() << endl << "Author: " << l.myBooks[i].getAuthor() << endl;
        }
        return out;
    }
}

Library.h

#ifndef LIBRARY_H
#define LIBRARY_H
#include <string>
#include "Book.h"
#include "BookNotFound.h"

using namespace std;
namespace CS20A
{
    class Library
    {
    public:
        Library();
        void addBookToCollection( string author, string title );
        void checkoutBook( string author, string title ) throw (BookNotFound);
        void checkinBook( string author, string title ) throw (BookNotFound);
        friend ostream& operator<< ( ostream& out, const Library & l );
    private:
        Book myBooks[ 20 ];
        int myNumberOfBooksSeenSoFar;
    };
}
#endif

Book.cpp

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

using namespace std;
namespace CS20A {
    Book::Book(string author, string title) {
        my_Author = author;
        my_Title = title;
    }
    string Book::getTitle() const {
        return(my_Title);
    }
    string Book::getAuthor() const {
        return(my_Author);
    }
    int Book::getNumberOfTimesCheckedOut() const {
        return(my_NumberOfTimesCheckedOut);
    }
    void Book::increaseNumberOfTimesCheckedOut(int amount) {
        my_NumberOfTimesCheckedOut = my_NumberOfTimesCheckedOut + amount;
    }
    ostream &operator<< (ostream& out, const Book & b){
        out << "Title: " << b.my_Title << endl;
        out << "Author: " << b.my_Author << endl;
        out << "Number of time checkout out: " << b.my_NumberOfTimesCheckedOut;
        return(out);
    }
}

Book.h

#ifndef BOOK_H
#define BOOK_H
#include <string>
using namespace std;
namespace CS20A
{
    class Book {
    public:
        Book();
        Book( string author, string title );
        string getTitle() const;
        string getAuthor() const;
        int getNumberOfTimesCheckedOut() const;
        void increaseNumberOfTimesCheckedOut( int amount=1 );
        friend ostream& operator<< ( ostream& out, const Book & b );
    private:
        string my_Author;
        string my_Title;
        int my_NumberOfTimesCheckedOut;
    };
};
#endif

BookNotFound.h

#ifndef BOOKNOTFOUND_H
#define BOOKNOTFOUND_H
#include <stdexcept>
#include <iostream>
#include <string>

namespace CS20A
{
    class BookNotFound : public std::logic_error
    {
    public:
        BookNotFound():logic_error("Error"){};
        BookNotFound(std::string e):logic_error(e){};
    };
};
#endif

Aucun commentaire:

Enregistrer un commentaire