gpt4 book ai didi

c++ - 使用头文件 : Undefined symbols for architecture x86_64 编译 C++ 程序时出错

转载 作者:行者123 更新时间:2023-11-28 06:20:23 25 4
gpt4 key购买 nike

我正在编写一个程序,该程序在单独的头文件中包含我的函数声明和定义以及结构定义。 .cpp 文件和 functions.h 文件都位于同一个文件夹中。但是,我总是收到臭名昭著的“Undefined symbols for architecture 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 的 undefined symbol :“functions::READ_INVENTORY(Books, int, int)”,引用自:_main 在 bookstore-9693df.oclang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)

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

最佳答案

您需要在定义函数时将其放在 READ_INVENTORY() 前面。所以 functions.h 中的定义看起来像:

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

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

关于c++ - 使用头文件 : Undefined symbols for architecture x86_64 编译 C++ 程序时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29404471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com