简体   繁体   English

Apple Mach O-Linker错误Xcode(C ++):体系结构x86_64的未定义符号

[英]Apple Mach O-Linker Error Xcode (C++): Undefined symbols for architecture x86_64

Why am I getting the following error with Xcode? 为什么Xcode出现以下错误?

Undefined symbols for architecture x86_64:
  "displayFile()", referenced from:
      _main in main.o
  "quitProgram(bool&)", referenced from:
      _main in main.o
  "editFile()", referenced from:
      _main in main.o
  "openFile()", referenced from:
      _main in main.o
  "saveFile()", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see 
invocation)

Here is the number_enforcer.hpp: 这是number_enforcer.hpp:

#ifndef number_enforcer_hpp
#define number_enforcer_hpp
 class Enforcer {
 private:
    int int_n;                                                                
    double n;
public:
     int natural_number_enforcer(int min, int max);
};
#endif /* number_enforcer_hpp */

The number_enforcer.cpp: number_enforcer.cpp:

#include "number_enforcer.hpp"
#include <iostream>
using namespace std;

//***********************************************************************
// Definition of function natural_number_enforcer                            *
//                                                                            *
// forces input for a variable to be a natural number.                        *
// WARNING: Does not work for numbers too large for type int (32 bits)    *
//***********************************************************************

int Enforcer::natural_number_enforcer(int min, int max)
{
    do
    {
        if (cin >> n)                                                            // input n and see if input is a number
        {                                                                        // If n is a number, then:
        cin.ignore(1000000000000000000, '\n');                                // if first characters form a number,...
        // ...this ignores any extra junk typed after that number.
        int_n = n;                                                            // assigns input of type double to int_n of type int
        // int_n is the truncated version of n (if n is a decimal not too large for type int)
        if (n != int_n || n < min || n > max)                                            // Test if n is a natural number (it should equal the truncated version int_n and be greater than 1).
        {                                                                    // Otherwise, n is not a natural number (or else n is too large for type int).
            cout << "\nError: Input needs to be a whole number between " << min << " and " << max << "." << endl;
        }
        else                                                                // if inputed n is actually a natural number, then:
        {
            break;                                                                // quit the do-while loop and keep the acceptable input for n
        }
    }
    else                                                                // If n is not a number, then:
    {
        cout << "\nA letter/punctuation is not a number.\n";                // tell the user that their input was not a number
        cin.clear();                                                        // clear the input (or else the program will go crazy and repeat a part forever)
        cin.ignore(1000000000000000000, '\n');                                // ignore potential extra inputed junk as well
    }
    cout << "Enter a new number:\t";                                // tell the user to input a natural number
    } while (true);                                                            // loop until user inputs a natural number
    return n;
}

And the main.cpp: 和main.cpp:

#include <iostream>
#include <fstream>
#include <string>
#include "number_enforcer.hpp"
using namespace std;
Enforcer enf; //calls to class Enforcer for managing input

//=======FUNCTIONS=======
void createFile(int (&x)[100]);
void saveFile();
void openFile();
void displayFile();
void editFile();
void quitProgram(bool &);
//=======================

int main() {
    int menuOption; //user-defined choice for Main Menu
    bool quit; //true --> quit program
    int scores[100]; //declare array for storing scores with a limit of 100 items

    do {
        //=======MAIN MENU=======
        cout << "What would you like to do?\n" <<
                "1. Create New File\n" <<
                "2. Save Current Information\n" <<
                "3. Open Another File\n" <<
                "4. Display Current Scores\n" <<
                "5. Modify Certain Scores\n" <<
                "6. Quit" << endl;
        menuOption = enf.natural_number_enforcer(1, 6);
        switch (menuOption) {
            case 1:
                createFile(scores);
                break;
            case 2:
                saveFile();
                break;
            case 3:
                openFile();
                break;
            case 4:
                displayFile();
                break;
            case 5:
                editFile();
                break;
            case 6:
                quitProgram(quit);
                break;
            default:
                break;
        }

    } while (!quit);

    return 0;
}

void createFile(int (&x)[100]) {
    int subMenuOption; //user-defined choice for Sub Menu
    int numberOfScores; //user-defined amount of scores to store
    cout << "Are you sure you want to create a new file?\n" <<
            "(This will overwrite any unsaved progress)\n\n" <<
            "1. Yes\n" <<
            "2. No" << endl;
    subMenuOption = enf.natural_number_enforcer(1, 2);
    if (subMenuOption == 1) {

        cout << "How many scores do you wish to enter (up to 100)?: \t";
        numberOfScores = enf.natural_number_enforcer(1, 100);
        for (int i = 0; i < numberOfScores - 1; i++) {
            cout << "Enter Score #" << i + 1 << ": \t";
            x[i] = enf.natural_number_enforcer(0, 100000);
        }
         for (int i = 0; i < numberOfScores - 1; i++) {
             cout << x[i] << endl;
         }
    }
}

I just started my project before noticing this strange error. 在注意到这个奇怪的错误之前,我只是开始了我的项目。 How to fix it? 如何解决?

I assume it has to do with my header files that I added, but I remember testing it before and it working. 我认为它与我添加的头文件有关,但是我记得之前对其进行过测试并且可以工作。 The only thing I added after that was the prototypes and the createFile() function, then suddenly it wouldn't build. 在此之后,我唯一添加的是原型和createFile()函数,然后突然无法构建。 I tried removing the createFile() function but to no avail. 我尝试删除createFile()函数,但无济于事。

You declared but not defined these functions: 您声明了但未定义以下功能:

void saveFile();
void openFile();
void displayFile();
void editFile();
void quitProgram(bool &);

You call the functions in your code, so linker tries to find their implementation and fails. 您在代码中调用了函数,因此链接器尝试查找其实现并失败。

Just add definitions for the functions as you did for createFile . 只需为函数添加定义,就像对createFile所做的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OpenCV Apple Mach-O链接器错误-体系结构x86_64的未定义符号 - OpenCV Apple Mach-O Linker Error - Undefined symbols for architecture x86_64 C++ 架构 x86_64 链接器错误的未定义符号 - Undefined symbols for architecture x86_64 Linker Error with C++ C++ 错误 - 体系结构 x86_64 的未定义符号: - C++ error - Undefined symbols for architecture x86_64: C ++错误:体系结构x86_64的未定义符号 - C++ error: Undefined symbols for architecture x86_64 C++ on Xcode 7.01 on El Capitan Error -&gt; 架构 x86_64 的未定义符号: - C++ on Xcode 7.01 on El Capitan Error -> Undefined symbols for architecture x86_64: 在 Xcode 上的 Swift 项目上使用 C++ 时出错:体系结构 x86_64 的未定义符号 - Error to use C++ on Swift project on Xcode: Undefined symbols for architecture x86_64 C ++的XCode错误“架构x86_64的未定义符号” - XCode Error “Undefined symbols for architecture x86_64” for C++ 架构 x86_64 的未定义符号:linker 错误 - Undefined symbols for architecture x86_64: linker error 体系结构x86_64参考链接器错误的未定义符号 - Undefined symbols for architecture x86_64 reference linker error 链接器错误:“架构 x86_64 的未定义符号” - Linker error: "Undefined symbols for architecture x86_64"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM