0

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!?

2
  • Sorry about the #includes up top. Apparently it filtered those out but I think they are inconsequential anyways.
    – mmmpop
    Sep 23, 2010 at 2:19
  • Got it! Forgot to make constructors. Silly me. Thanks for the quick help guys you're simply the best. Hope I can contribute myself soon.
    – mmmpop
    Sep 23, 2010 at 2:26

3 Answers 3

5

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).

2
  • 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
    – mmmpop
    Sep 23, 2010 at 2:20
  • 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!
    – mmmpop
    Sep 23, 2010 at 2:25
1

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

1

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
2
  • 1
    Or just don't explicitly declare a constructor if you don't need to. Sep 23, 2010 at 2:16
  • 1
    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. Sep 23, 2010 at 2:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.