繁体   English   中英

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

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

为什么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)

这是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 */

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

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

在注意到这个奇怪的错误之前,我只是开始了我的项目。 如何解决?

我认为它与我添加的头文件有关,但是我记得之前对其进行过测试并且可以工作。 在此之后,我唯一添加的是原型和createFile()函数,然后突然无法构建。 我尝试删除createFile()函数,但无济于事。

您声明了但未定义以下功能:

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

您在代码中调用了函数,因此链接器尝试查找其实现并失败。

只需为函数添加定义,就像对createFile所做的那样。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM