2015-04-02 48 views
0

我正在编写一个程序,该函数在一个单独的头文件中包含函数声明和定义以及结构定义。 .cpp文件和functions.h文件都位于同一个文件夹中。但是,我始终得到臭名昭着的“架构x86_64的未定义符号:”错误。我已经阅读了许多关于这个错误的其他问题,但我无法解决这个问题。这里是代码:使用头文件编译C++程序时出错:体系结构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: “functions :: READ_INVENTORY(Books,int,int)”,引用来自: _main in bookstore-96 93df.o 铛:错误:连接命令失败,退出代码1(使用-v看看调用)

所以这是这个归结为:这是错误造成怎样的程序被写入或由外部冲突与程序无关?

+0

C++不是强制执行* pure *面向对象编程的语言。以有意义的方式设计你的类,不要仅仅为了保持功能而创建类。 – 2015-04-02 04:23:31

+0

此外,你显示的代码甚至不会编译,因为你传递的是一个需要指针的函数的引用。 – 2015-04-02 04:24:58

回答

1

当您定义它时,您需要将函数::放在READ_INVENTORY()前面。因此,在functions.h中的定义如下所示:

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

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

+0

谢谢!这有助于我终于让我的程序编译。我刚刚开始,并从这些乏味的任务中学到很多东西。 – jshapy8 2015-04-02 05:03:15

相关问题