1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

undefined reference Error!

Discussion in 'C++' started by NatProg, Apr 19, 2012.

  1. NatProg

    NatProg New Member

    Joined:
    Apr 19, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Would someone help me understand why I keep getting the error:
    "In function main: [linker error] undefined reference to readBook(BookData)"

    I have spend hours trying to fix it, but no luck :/

    Thanks in advance

    main.cpp :
    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    using namespace std;
    #include "BookData.h"
    
    // FUNCTION PROTOTYPES:
    void bookInfo(BookData);
    void readBook(BookData& book1);
    
    int main(){
        BookData book1, book2;
        cout << "\nEnter the data for the first book:\n";
        readBook(book1);
        cout << "\nEnter the data for the second book:\n";
        readBook(book2);
        cout << "\nDisplay the data for the first book:\n";
        bookInfo(book1);
        cout << "\nDisplay the data for the second book:\n";
        bookInfo(book2);
        system("pause");
        return 0;
    }
    void bookInfo(BookData book)
    {
         string temp;
         cout << left;
         cout << "   " << setw(22) << "ISBN:" << book.getIsbn() << endl;
         cout << "   " << setw(22) << "Title:" << book.getTitle() << endl;
         cout << "   " << setw(22) << "Author:" << book.getAuthor() << endl;
         cout << "   " << setw(22) << "Publisher:" << book.getPub() << endl;
         cout << "   " << setw(22) << "Date Added:" << book.getDateAdded() << endl;
         cout << "   " << setw(22) << "Quantity-On-Hand:" << book.getQty() << endl;
         cout << "   " << setw(22) << "Wholesale Cost:" << "$" << fixed << setprecision(2) << book.getWholeSale() << endl;
         cout << "   " << setw(22) << "Retail Price:" << "$" << book.getRetail() << endl;
         temp = book.isEmpty()?"yes":"No";
         cout << "   " << setw(22) << "Empty?" << temp << endl;
    }
    void readBook(BookData& book1)
    {
         string tempS;
         int tempI;
         double tempD;
         
         cout << "Enter the Title: ";
         cin >> tempS;
         book1.setTitle(tempS);        
         
         cout << "Enter the ISBN: ";
         cin >> tempS;
         book1.setIsbn(tempS);
         
         cout << "Enter the Author: ";
         cin >> tempS;
         book1.setAuthor(tempS);
         
         cout << "Enter the Publisher: ";
         cin >> tempS;
         book1.setPub(tempS);
         
         cout << "Enter Date Added: " ;
         cin >> tempS;
         book1.setDateAdded(tempS);
         
         cout << "Enter Quantity on Hand: ";
         cin >> tempI;
         book1.setQty(tempI);
         
         cout << "Enter Wholsale Price: " ;
         cin >> tempD;
         book1.setWholesale(tempD);
         
         cout << "Enter Retail Price: " ;
         cin >> tempD;
         book1.setRetail(tempD);
         
         cout << "Enter 1 to insert the book or 0 to remove: ";
         cin >> tempI;
         while (tempI > 1 || tempI < 0)
         {
               cout << "Wrong entry. Try again: ";
               cin >> tempI;
         }
         if (tempI == 0)
             book1.insertBook();
         else
             book1.removeBook();     
    }
    
    


    BookData.cpp :
    Code:
    #include <string>
    
    class BookData
    {
        private:
            string bookTitle;
            string isbn;
            string author;
            string publisher;
            string dateAdded;
            int qtyOnHand;
            double wholesale;
            double retail;
            bool empty;
            
        public:
            BookData();
            void setTitle(string object);
            void setIsbn(string code);
            void setAuthor(string auth);
            void setPub(string pub);
            void setDateAdded(string dateAdd);
            void setQty(int quant);
            void setWholesale(double price);
            void setRetail(double temp);
            bool isEmpty();
            void insertBook();
            void removeBook();
            string getTitle();
            string getIsbn();
            string getAuthor();
            string getPub();
            string getDateAdded();
            int getQty();
            double getWholeSale();
            double getRetail();
    };
    
    BookData::BookData()//Default Constructor
    {
      qtyOnHand = 0;
      wholesale = 0;
      retail = 0;
      empty = true;
      bookTitle = isbn =  author =  publisher = dateAdded = "";
    }
    
    void BookData::setTitle(string object)
    {
        bookTitle = object;
    }
    
    void BookData::setIsbn(string code)
    {
        isbn = code;
    }
    
    void BookData::setAuthor(string auth)
    {
        author = auth;
    }
    
    void BookData::setPub(string pub)
    {
        publisher = pub;
    }
    
    void BookData::setDateAdded(string dateAdd)
    {
        dateAdded = dateAdd;
    }
    
    void BookData::setQty(int quant)
    {
        qtyOnHand = quant;
    }
    
    void BookData::setWholesale(double price)
    {
        wholesale = price;
    }
    
    void BookData::setRetail(double temp){
           retail = temp;
    }
    bool BookData::isEmpty(){
         return empty;
    }
    void BookData::insertBook(){
         empty = false;
    }
    void BookData::removeBook(){
         empty = true;
    }
    string BookData::getTitle(){
           return bookTitle;
    }
    string BookData::getIsbn(){
        return isbn;
    }
    string BookData::getAuthor(){
           return author;
    }
    string BookData::getPub(){
            return publisher;
    }
    string BookData::getDateAdded(){
           return dateAdded;
    }
    int BookData::getQty(){
        return qtyOnHand;
    }
    double BookData::getWholeSale(){
           return wholesale;
    }
    double BookData::getRetail(){
           return retail;
    }
    
    DataBook.h :
    Code:
    #ifndef BOOKDATA_H
    #define BOOKDATA_H
    #include <string>
    
    class BookData
    {
        private:
            string bookTitle;
            string isbn;
            string author;
            string publisher;
            string dateAdded;
            int qtyOnHand;
            double wholesale;
            double retail;
            bool empty;
            
        public:
            BookData();
            void setTitle(string object);
            void setIsbn(string code);
            void setAuthor(string auth);
            void setPub(string pub);
            void setDateAdded(string dateAdd);
            void setQty(int quant);
            void setWholesale(double price);
            void setRetail(double temp);
            bool isEmpty();
            void insertBook();
            void removeBook();
            string getTitle();
            string getIsbn();
            string getAuthor();
            string getPub();
            string getDateAdded();
            int getQty();
            double getWholeSale();
            double getRetail();
    };
    #endif
            
            
    
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    I get no error (although I had to add "using namespace std;" to BookData.h) with Visual Studio 2010. What compiler and OS are you using (including version numbers)?

    Also I would suggest not defining "class BookData" twice; just define it once in BookData.h, then #include BookData.h wherever you need it. Otherwise You will need to ensure both copies of class BookData remain consistent.
     
  3. NatProg

    NatProg New Member

    Joined:
    Apr 19, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Yes. you are right. I was using the Dev C++, and had no idea I need to put them in a project to be able to run it! Thanks for the advice. I added the namespace, and erased the extra parts and it worked.
     

Share This Page