簡體   English   中英

鏈接器錯誤:“架構 x86_64 的未定義符號”

[英]Linker error: "Undefined symbols for architecture x86_64"

我正在研究一個骰子類,它允許用戶輸入一個骰子的邊數並輸出每個骰子的卷數和結果數。 我對類很陌生,我知道我把它們搞砸了,但現在無法處理它們,因為鏈接器在 Xcode 和代碼塊中給了我一個錯誤。

確切的錯誤是:

Undefined symbols for architecture x86_64:
  "GameDie::GameDie()", 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)

這是我的程序代碼:

 #include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int x = 0;
int sides;

class GameDie
{
public:
    GameDie();
    int roll();
    void getNumSides(int numSides);
    void getNumRolls();
private:
    int numRolls;
    int numSides;
};


int main()
{
    srand(time(0));
    cout << "Enter number of sides: ";
    cin >> sides;

    GameDie die1;
    die1.roll();
    die1.getNumSides(sides);
    die1.getNumRolls();

    return 0;
}


int GameDie::roll()
{
    return (rand() % sides) + 1;
}

void GameDie::getNumSides(int sides)
{
    numSides = sides;
}

void GameDie::getNumRolls()
{
    x++;
    cout << "Roll " << x << " of die with " << sides << " sides";
}

不太確定這個模具類應該如何運行,但我使用了您的代碼並盡可能少地更改以使其正常運行。 這主要是方法/成員函數的錯誤構造。 我希望這有幫助。

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int x = 0;
int sides;

class GameDie
{
public:
    GameDie();
    int roll();
    void getNumSides(int numSides);
    void getNumRolls();
private:
    int numRolls;
    int numSides;
};


int main()
{
    srand(time(0));
    cout << "Enter number of sides: ";
    cin >> sides;

    GameDie die1;
    die1.roll();
    die1.getNumSides(sides);
    die1.getNumRolls();

    return 0;
}

GameDie::GameDie()
{
    numSides = 0;
}

int GameDie::roll()
{
    return (rand() % sides) + 1;
}

void GameDie::getNumSides(int sides)
{
    numSides = sides;
}

void GameDie::getNumRolls()
{
    x++;
    cout << "Roll " << x << " of die with " << sides << " sides";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM