3 Replies - 523 Views - Last Post: 19 April 2012 - 09:36 PM Rate Topic: -----

#1 NatProg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-April 12

undefined reference Error!

Posted 19 April 2012 - 12:03 PM

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 :
#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 :
#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 :
#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
        
        



Is This A Good Question/Topic? 0
  • +

Replies To: undefined reference Error!

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 862
  • View blog
  • Posts: 3,982
  • Joined: 09-June 09

Re: undefined reference Error!

Posted 19 April 2012 - 12:16 PM

Why do you re declare the BookData class into your source file (BookData.cpp) ? Just include the header BookData.h at the top of BookData.cpp
Was This Post Helpful? 0
  • +
  • -

#3 NatProg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-April 12

Re: undefined reference Error!

Posted 19 April 2012 - 12:52 PM

View Postjjl, on 19 April 2012 - 12:16 PM, said:

Why do you re declare the BookData class into your source file (BookData.cpp) ? Just include the header BookData.h at the top of BookData.cpp


Oh right, I just added the #include "BookData.h" to my BookData.cpp, BUT I still get the same "undefined reference" error!
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 974
  • View blog
  • Posts: 3,390
  • Joined: 19-February 09

Re: undefined reference Error!

Posted 19 April 2012 - 09:36 PM

Hi. You've removed the class section from BookData.cpp?
You have added BookData.cpp to your project?
The header file is named BookData.h and not DataBook.h as in your first post?

You could try adding using namespace std; to the beginning of BookData.h.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1