使用头文件编译 C++ 程序时出错:体系结构的未定义符号 x86_64

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

我正在编写一个程序,该程序在单独的头文件中包含我的函数声明和定义以及结构定义。 .cpp 文件和 functions.h 文件都位于同一文件夹中。但是,我一直收到臭名昭著的“未定义的体系结构符号 x86_64:”错误。我已经阅读了许多关于此错误的其他问题,但我无法解决此问题。这是代码:

我正在使用 Mac OSX 中的终端进行编码。我是运行Yosemite。但是,我在 PuTTY 中将它复制到 Windows 和 运行 中,并且出现了完全相同的错误消息。

对于functions.h头文件:

#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

这是 .cpp 文件:

#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;
}

最后,这是完整的错误信息:

未定义的体系结构符号x86_64: "functions::READ_INVENTORY(Books, int, int)",引用自: _main 在书店-9693df.o clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)

归结为:这个错误是由程序的编写方式引起的,还是由与程序无关的外部冲突引起的?

定义函数时,需要在READ_INVENTORY()前加上functions::。所以 functions.h 中的定义看起来像:

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

在 .cpp 文件中实现这些函数通常是更好的做法。