-2

I am working on a die class that allows the user to input the number of sides on a die and outputs the number of rolls per die and the resulting number. I am very new to classes and I know I have them messed up, but can't work through them right now because the linker is giving me an error in Xcode and codeblocks.

The exact error is:

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)

And here is my program code:

 #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";
}
Fehler
  • 17
  • 1
  • 4
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – user0042 Oct 27 '17 at 04:15
  • `int roll() { ... }` or `int GameDie::roll() { ... }`? You do it correct for `getNumSides`, why not the other member functions? – Some programmer dude Oct 27 '17 at 04:17
  • you need to put `void GameDie::getNumRolls()` – yacc Oct 27 '17 at 04:20
  • Good catch @Someprogrammerdude. Fixed it, though it doesn't have an effect on the linker problem. – Fehler Oct 27 '17 at 04:21
  • Fixed the other one too @yacc, thank you. Updated the error message, but don't know what that one could refer to... – Fehler Oct 27 '17 at 04:25
  • It means you don't have implemented (defined) the *constructor*. – Some programmer dude Oct 27 '17 at 04:27

1 Answers1

0

Not really sure how this die class should operate but I took your code and changed as little as possible to make it functional. It was mostly misconstruction of the methods/member-functions. I hope this helps.

#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";
}
Roy Lara
  • 71
  • 2
  • 1
    Ah, now it's painfully obvious. For some reason the book I'm reading didn't show such an example until the next section. Thanks for your help @Roy Lara and everybody! – Fehler Oct 27 '17 at 04:58