views:

52

answers:

3

I'm a pretty seasoned programmer but I'm just now diving into C++ and it's... well... more difficult than PHP and Python. I keep having unresolved external errors when trying to create an object from some classes. It's broken up into multiple headers and files but here is a basic idea from one of my classes:

die.h:

#ifndef DIE_H
#define DIE_H

using namespace std;

class Die {
 public: 
  int throwDie();
  Die();
};

#endif

die.cpp

#include <iostream>
#include <cstdlib>
#include "Die.h"

using namespace std;

int Die::throwDie() 
{
 return 0;
}

sixsidedie.h

#ifndef SIXSIDEDIE_H
#define SIXSIDEDIE_H

#include "Die.h"

using namespace std;

class SixSideDie : public Die
{
 public:
  SixSideDie();
  int throwDie();

 private: 
         int randNumber;
};

#endif

sixsidedie.cpp

#include <iostream>
#include <cstdlib>
#include <time.h>
#include "Die.h"
#include "SixSideDie.h"

using namespace std;

const int SIX_SIDE = 6;

int SixSideDie::throwDie()
{
 srand((unsigned int)time(0));
 SixSideDie::randNumber = rand() % SIX_SIDE + 1;
 return SixSideDie::randNumber;
}

main.cpp

#include <iostream>
#include <cstdlib>
#include "Die.h"
#include "SixSideDie.h"
#include "TenSideDie.h"
#include "TwentySideDie.h"

using namespace std;

int main() 
{
 Die* myDice[3];
 myDice[0] = new SixSideDie();
 myDice[1] = new TenSideDie();
 myDice[2] = new TwentySideDie();

 myDice[0]->throwDie();
 myDice[1]->throwDie();
 myDice[2]->throwDie();

 system("pause");
 return 0;
}

It keeps telling me that each object I create directly above is an unresolved external symbol and I just don't know why. Any thoughts!?

+5  A: 

You declared a constructor for Die but never defined it.

Also, you almost certainly want throwDie to be virtual if you intend to override its behavior in derived classes, and you should never use using namespace std; in a header file (and many people, including me, would argue that you shouldn't use it at file-scope at all).

James McNellis
+1, good catch... `:)`
Prasoon Saurav
You're right about the virtual! That's actually the next step in the lab but I have to get the program to compile first. I'll try that and report back. Inheritance is still a bit mind-boggling to me! :D
nanerj
Well you were all 100% correct but I'll give you props on the namespace tip. I'm sure that would have cost me some points tomorrow!
nanerj
+1  A: 

You didn't define your constructor in your cpp files.

Eric Fortin
+1  A: 

Its good practice to define the constructors of the classes. Check this out:

#ifndef DIE_H
#define DIE_H

using namespace std;

class Die {
 public: 
  int throwDie();
  Die() { };  // can you spot the difference here?
};

#endif
karlphillip
Or just don't explicitly declare a constructor if you don't need to.
James McNellis
@McNellis indeed.
karlphillip
Just a heads-up: If you use the @name, it has to be the first characters of the user's name, otherwise the user won't get a notification.
James McNellis
@James Thanks!!
karlphillip