2017-11-28 44 views
1

私はX-コードとはかなり新しいですし、私はこのエラーを取得していますなぜ思っていた。ここでアップルマッハO-リンカエラーX-コード(C++)

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()関数を削除しようとしましたが、無駄にしました。

答えて

0

あなたは宣言されていますが、これらの関数を定義していない:

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

あなたはあなたのコード内の関数を呼び出すので、リンカがその実施を見つけようとすると失敗します。

createFileの場合と同じように、関数の定義を追加するだけです。

+0

それでした。ソリューションがとてもシンプルだったとは信じられない – gvsv

関連する問題