0

Help! I am new to C++ programming and came across a nightmare bug when creating 3 files: book.h, book.cpp, and Mp8bookDriver.cpp. The book.h sets up the default constructor and barebones of a class function called "Book". book.cpp contains the substance of the functions associated with the class. Mp8bookDriver.cpp contains the main function.

The error is Undefined symbols for architecture x86_64: "Book::GetData(std::__1::basic_string, std::__1::allocator >)", referenced from: _main in Mp8bookDriver-8ad215.o "Book::Book()", referenced from: _main in Mp8bookDriver-8ad215.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

Here are the code files:

//book.h
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

#ifndef BOOK_H
#define BOOK_H
class Book
{
    private:
        string bookAuthor;
        string bookTitle;
        long int bookISBN;
    public:
        //Default Constructor
        Book();
        // //Overload Constructor
        // Book(string, string, long int);
        //Accessor Functions
        void GetData(string fName); 
        //long int GetISBN(long int bookISBN);
        //void printBookInfo(string bookAuthor, string bookTitle, long int bookISBN) const; //add params if necessary

};
#endif
//book.cpp
#include <iostream>
#include "book.h"
#include <string>
#include <fstream>
#include <vector>

using namespace std;

Book::Book(){
//setting variables to their null state
    bookAuthor = "No Name";
    bookTitle = "Unknown Title";
    bookISBN = 0;

}
////overload constructor


// void Book::printBookInfo(const string & bookAuthor, const string &bookTitle, const long int & bookISBN){
//     cout >> "The information for book ">> counter >> " is:">>endl; //FINISH LATER
//     cout>> this-> bookAuthor >> " ">> this-> bookTitle >> " " >> this-> bookISBN>>endl;
//     if (bookISBN!= 0){
//         cout>> "book " >> counter >> " has ISBN " >> this-> bookISBN>>endl;
//     }
// }

void Book::GetData(string fName){
    string line;
    string tempVar;
    long int tester;
    ifstream inFile;
    int counter;
    inFile.open(fName);
    if (inFile.fail()){ 
        cout << "Failed to open input or output file. Try again.";
        exit(1);
    }
    else{
        cout<< "YEAH!";
    }
    // while (!inFile.eof()){
    //     counter+=1;
    //     getline(inFile, line);
    //     if (counter == 1){
    //         inFile >> bookAuthor;
    //         cout << bookAuthor<<endl;
    //     }
    //     else if (counter == 1){
    //         inFile>> bookTitle;
    //         cout<<bookTitle<<endl;
    //     }
    //     else if (counter == 3){
    //         inFile >> tempVar;
    //         tester= atol(tempVar.c_str());
    //         cout<<bookISBN<<endl;
    //         cout<< "\tType ID of ISBN: " << typeid(bookISBN).name()<< endl;
    //     }
    //     else{
    //         cout<<"\n\tMistake in counter or lines\n" <<endl;
    //         cout <<"\n"<<endl;
    //     }
    // }
    inFile.close();
    //printBookInfo(bookAuthor, bookTitle, bookISBN);
}
//still need to establish default constructor and initialize author's 
//name to "No Name" and title to "Unknown Title" and ISBN to 0
// need to create functions
//Mp8bookDriver.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "book.h"

using namespace std;

int main(){
    string fName = "mp7book.txt";
    string author;
    string title;
    long int isbn;
    Book book1; //use default constructor
    //cout >> book1.printBookInfo();
    Book book2;//use default constructor
    book2.GetData(fName);



    // cout>> book1.GetData()>>endl;
    // cout>>book2.GetData()>>endl;





    return 0;
}

Some portions of code are tagged out to narrow down the problem. Thank you!

Madel
  • 1
  • 2
  • 1
    You are not actually compiling and/or linking `book.cpp`. Check your project's build configuration. – Igor Tandetnik Jun 01 '20 at 03:26
  • @IgorTandetnik how would I check that? – Madel Jun 01 '20 at 03:27
  • 1
    I don't know, I'm not familiar with VSCode. Somewhere in there is a list of source files that should be compiled and linked together. Make sure both your source files are mentioned there. – Igor Tandetnik Jun 01 '20 at 03:30
  • 1
    Unfortunately, you are the only one who knows which compiler you are using, and how you configured it. Nobody else on stackoverflow.com would have any idea. – Sam Varshavchik Jun 01 '20 at 03:30
  • Igor and Sam are great at helping if you give them food. What have **you** done to solve the problem? – Ted Lyngmo Jun 01 '20 at 03:43

0 Answers0