0

I'm working on something personal as an exercise with classes. As background, I basically just want to make a class for players of a board game, to keep track of the order of their turns based on some simple calculations using their stats.

My Player class includes this constructor line:

Player(string name, int Dex, int Mod, int Lvl, int diceRoll);

Its private data is as follows:

int Dex, Mod, Lvl;
string name;

I have the following in my main function, and have included the iostream and string libraries.

int rollD; //Will be input by the user
Player Derek("Derek", 2, 0, 6, rollD);
//... etc.

The error that the compiler throws is precisely this:

Initiative.obj : error LNK2019: unresolved external symbol "public: __thiscall Player::Player(class std::basic_string,class std::allocator >,int,int,int,int)" (??0Player@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HHHH@Z) referenced in function _main

I am using Visual Studio 2012. From what I can tell, it's taking issue with the syntax of my constructor call, though I believe it to be correct.

Can anyone help me out? I've looked through the other threads with "External Symbol" errors, but I don't appear to have done the things that caused their errors, as my code is very simple.

Thanks!

EDIT: New error. I've changed the name of my private members in the class to be more readily identifiable (mDex, for example). So I have the following:

Player::Player(string name, int Dex, int Mod, int Lvl, int diceRoll) {
              mName = name;
      mDex = Dex; 
      mMod = Mod;
      mLvl = Lvl;
}

Now my error is in the string name parameter. It seems to define name as the type, not the variable, and says I am not allowed to use type name.

EDIT 2: Full code below.

#include <iostream>
#include <string>

using namespace std;

class Player {
  public:
Player(string name, int Dex, int Mod, int Lvl, int diceRoll);

int calcInitiative(int Dex, int Mod, int Lvl);
int sortInitiative(int Init);

int diceRoll;

  private:

  int mDex, mMod, mLvl;
  string mName;
};

int main() {
int rollD; //To be given by the user later.

Player::Player(string name, int Dex, int Mod, int Lvl, int diceRoll) {
mName(name), mDex(Dex), mMod(Mod), mLvl(Lvl);
}

Player Derek("Derek", 2, 0, 6, rollD);

return 0;
}
Rome_Leader
  • 2,518
  • 9
  • 42
  • 73
  • 3
    `but I don't appear to have done the things that caused their errors, as my code is very simple` you didn't implement the method, did you? – Luchian Grigore May 30 '13 at 01:14
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – chris May 30 '13 at 01:24
  • Update with the edit: You should use constructor initializer lists to initialize your members. – chris May 30 '13 at 01:32
  • I see that it is a cleaner, more compact approach but it does not resolve my error. – Rome_Leader May 30 '13 at 01:35

1 Answers1

2

You have to implement the constructor for Player.

Defined inside class:

class Player{
  //...other stuff
  Player(string name, int Dex, int Mod, int Lvl, int diceRoll):
          name_(name), Dex_(Dex), Mod_(Mod), Lv1_(Lv1), diceRoll_(diceRoll){}
};
//^^^Assume name_ and others are your member variables

Defined outside class:

Player::Player(string name, int Dex, int Mod, int Lvl, int diceRoll):
          name_(name), Dex_(Dex), Mod_(Mod), Lv1_(Lv1), diceRoll_(diceRoll)
{}

Otherwise, the compiler cannot find the definition of your constructor.

taocp
  • 23,276
  • 10
  • 49
  • 62
  • Ooohhh, silly me! Right, so if I don't have the constructor implemented outside the class, of course it would not be defined for the main function. Thanks! – Rome_Leader May 30 '13 at 01:19
  • @user2395694 Yes, Note that it is OK to define inside class. – taocp May 30 '13 at 01:21
  • Huh. Now it is telling me in my outside the class function definition that I'm not allowed to use 'type name'. I have as a parameter for the constructor string name So, why is it giving me this error? – Rome_Leader May 30 '13 at 01:26
  • Done. The error is for the string name parameter, it seems to think name is the type instead of the parameter. – Rome_Leader May 30 '13 at 01:32
  • @user2395694 have you included `string` header? You'd prefer using member initialization list. – taocp May 30 '13 at 01:33
  • #include is there at the top of my code, yes. I also briefly tried #include to see if there was any change. No luck. – Rome_Leader May 30 '13 at 01:37
  • 1
    @user2395694, `` is for C string functions. And are you indicating that `string` resides in `std`? – chris May 30 '13 at 01:37
  • I just wondered if it would make a difference. No, simply that my first two lines are including the iostream and string libraries appropriately. – Rome_Leader May 30 '13 at 01:39
  • @user2395694 you should use std::string, since string is in namesapce std – taocp May 30 '13 at 01:40
  • I also have a using namespace std; line. Wouldn't that absolve me from having to do that every time I wanted to use a string? I was probably mistaken about the utility of including , but I am also using the std namespace. – Rome_Leader May 30 '13 at 01:42
  • @user2395694 weird. You have other codes not shown? The error may be in some other places. – taocp May 30 '13 at 01:45
  • I'll post the full thing for clarity, I guess. I would've initially, but I thought just focusing on the parts giving my initial error would be best. I'll update the original post. – Rome_Leader May 30 '13 at 01:48