简体   繁体   中英

Error when compiling C++ program with header file: Undefined symbols for architecture x86_64

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM