0

I am writing a program that has my function declarations and definitions in a separate header file along with a struct definition. Both the .cpp file and the functions.h file are located in the same folder. However, I consistently get the infamous "Undefined symbols for architecture x86_64:" error. I have read many other questions regarding this error, but I cannot get this resolved. Here is the code:

I am coding using the Terminal in Mac OSX. I am running Yosemite. However, I copied it into Windows and ran it in PuTTY and the same exact error message occurred.

For functions.h header file:

#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

struct Books
{
        int ISBN;
        string Author;
        string Publisher;
        int Quantity;
        double Price;
};


class functions
{
        public:
                void READ_INVENTORY(Books, int, int);

};


// Function definitions

void READ_INVENTORY(Books* list, int max, int position)
{
        cout << "You have chosen to read inventory from a file.\n"
             << "Press ENTER to continue...\n";
        cin.get();

        ifstream inputFile;
        inputFile.open("inventory.dat");
        if (!inputFile)
                cout << "Error: Input file cannot be found\n";
        else
        {
                inputFile >> list[position].ISBN;
                inputFile >> list[position].Author;
                inputFile >> list[position].Publisher;
                inputFile >> list[position].Quantity;
                inputFile >> list[position].Price;

            cout << "The following data was read from inventory.dat:\n\n"
                 << "ISBN: " << list[position].ISBN << endl
                 << "Author: " << list[position].Author << endl
                 << "Publisher: " << list[position].Publisher << endl
                 << "Quantity: " << list[position].Quantity << endl
                 << "Price: " << list[position].Price << endl << endl;

            cout << "Press ENTER to return to the main menu...\n";
            cin.get();
        }
}

#endif

And here is the .cpp file:

#include <iostream>
#include <string>
#include <fstream>
#include "functions.h"
using namespace std;

int main()
{
    const int MAX_SIZE = 100;
    int size, choice;
    functions bookstore;
    Books booklist[MAX_SIZE];

            cout << "Thank you for using Justin's Bookstore Manager\n\n";
    do
    {
            cout << "Please select a choice from the menu below:\n\n"

                 << "\t\t    MENU\n"
                 << "\t----------------------------\n\n"
                 << "\t1: Read inventory from a file\n"

            cin >> choice;

            size = choice;

            switch (choice)
            {
                    case 1: bookstore.READ_INVENTORY(booklist[choice], MAX_SIZE, size);

                            break;
                    default:
                    {
                            cout << "Sorry, that is not a valid selection\n\n";

                    }
            }
    } while (choice != 6);


    return 0;
}

Finally, here is the entire error message:

Undefined symbols for architecture x86_64: "functions::READ_INVENTORY(Books, int, int)", referenced from: _main in bookstore-9693df.o clang: error: linker command failed with exit code 1 (use -v to see invocation)

So here is what this comes down to: Is this error caused by how the program is written or by an outside conflict not related to the program?

jshapy8
  • 1,983
  • 7
  • 30
  • 60
  • C++ is not a language that enforces *pure* object oriented programming. Design your classes in a way that makes sense, don't make classes just for the sake of holding functions. – Ben Voigt Apr 02 '15 at 04:23
  • Also, the code you showed wouldn't even compile, since you're passing a reference to a function that expects a pointer. – Ben Voigt Apr 02 '15 at 04:24

1 Answers1

1

Your need to put functions:: in front of READ_INVENTORY() when you define it. So the definition in functions.h would look like:

void functions::READ_INVENTORY(Books* list, int max, int position)
{
    /* do stuff */
}

It's usually better practice to implement these functions in a .cpp file.

Jon
  • 26
  • 2
  • Thanks! This helped and I finally got my program to compile. I am just starting out and am learning so much from these tedious tasks. – jshapy8 Apr 02 '15 at 05:03