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

时间:2015-04-02 03:22:58

标签: c++ linker header-files undefined-symbol

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

我在Mac OSX中使用终端进行编码。我正在运行优胜美地。但是,我将其复制到Windows并在PuTTY中运行它并发生了同样的错误消息。

对于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的未定义符号: &#34; functions :: READ_INVENTORY(Books,int,int)&#34;,引自: _main in bookstore-9693df.o clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)

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

1 个答案:

答案 0 :(得分:1)

在定义函数时,需要将函数::放在READ_INVENTORY()之前。所以functions.h中的定义如下:

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

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