1

Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?

I am trying to code a college assignment and seem to having problems compiling, i have done a Google search on the error and the fixes I've seen don't work with my code. I would appreciate your help.

Below is the error code :

Error 1 error LNK2019: unresolved external symbol "public: __thiscall user::user(void)" (??0user@@QAE@XZ) 
referenced in function "void __cdecl `dynamic initializer for 'player''(void)" (??__Eplayer@@YAXXZ) 
C:\Users\obinyans\Documents\Visual Studio 2010\Projects\test\test\Challenge 1.obj

and below is a copy of my code:

#include <iostream>
#include <string>
#include <math.h>

using namespace std;

void storeinfo() ;
void showinfo() ;


class user 
{
    string firstname, lastname, currentteam, position, status ;
    int age ;
public:
    user();
    user(string, string, string, string, string, int) ;
    void setFirstName(string fname)
        {firstname = fname;}
    void setLastName(string lname)
        {lastname = lname;}
    void setCurrentTeam(string cteam)
        {currentteam = cteam;}
    void setPosition(string pos)
        {position = pos;}
    void setStatus(string stat)
        {status = stat;}
    void setAge(int _age)
        {age = _age;}

    string getFirstName()
        {return firstname ;}
    string getLastName()
        {return lastname ;}
    string getCurrentTeam()
        {return currentteam ;}
    string getPosition()
        {return position ;}
    string getStatus()
        {return status ;}
    int getAge()
        {return age ;}
};

user player[20] ;

int main()
{
     ;

    int menu ;

    cout << "MENU" << "\n" ;
    cout << "\n 1. Store Player Information" ;
    cout << "\n 2. Show Player Informaton" ;
    cout << "\n 0. Exit" ;


    cin >> menu ;
    if (menu = 1)
    {
        storeinfo() ;
    }
    else if (menu = 2)
    {
        showinfo() ;
    }
    else if (menu = 0)
    {
        return 0;
    }


    cin.get() ;
    return 0 ;

}

void storeinfo()
{
    string firstname ;
    string lastname ;
    string currentteam ;
    string position;
    string status ;
    int age ;

    for (int i=0; i < 3; i++)
    {
        cout << "Enter First Name : " ; cin >> firstname ;
        player[i].setFirstName(firstname) ;
        cout << "Enter Last Name : " ; cin >> lastname ;
        player[i].setLastName(lastname) ;
        cout << "Enter Player's Age : " ;cin >> age;
        player[i].setAge(age) ;
        cout << "Enter Current Team : " ; cin >> currentteam ;
        player[i].setCurrentTeam(currentteam) ;
        cout << "Enter Position : " ; cin >> position ;
        player[i].setPosition(position) ;
        cout << "Enter Status : " ; cin >> status ;
        player[i].setStatus(status) ;
    }
}

void showinfo()
{
    for (int i=0; i < 3; i++)
    {
        cout << "First Name : " << player[i].getFirstName() << "        " << "Last Name : " << player[i].getLastName() <<
            "       " << "Age : " << player[i].getAge() << "        " << "Current Team : " << player[i].getCurrentTeam() << 
            "       " << "Position : " << player[i].getPosition() << "      " << "Status :  " << player[i].getStatus() ;
    }
}

Thanks for any help.

Community
  • 1
  • 1
tarantino
  • 165
  • 1
  • 5
  • 14

2 Answers2

5

You didn't provide an implementation for your constructors:

class user
{
....
public:
  user();
  user(string, string, string, string, string, int);
....
};

Try adding bodies for both of them:

class user
{
....
public:

  // Default constructor
  user()
     // std::string's are default constructed to empty strings.
     // You may want to set a value for age data member.
    : _age(0)
  {
  }

  // Constructor with initialization parameters
  user(string first_name, string last_name, string current_team, 
       string position, string status, int age)
    : _first_name(move(first_name)), 
      _last_name(move(last_name)), 
      _current_team(move(current_team)),
      _position(move(position)), 
      _status(move(status)),
      _age(age)
  {
  }

....
};

Note that it is convenient to give some prefix to data members, to distinguish them from parameters. Some conventions are prefixing with _ or m_ (e.g. name is a parameter, _name or m_name is a data member).

Note also that in C++11 (and from your error code I read that you're using VS2010, which has some form of move semantics available), when you pass a class that is movable and is cheap to move, and you have to make a local copy (like when setting data members), you can pass by value and std::move from the value, e.g.

void set_first_name(string first_name)
{
    _first_name = move(first_name);
}

Note also that getters tend to be const:

const string& get_first_name() const
{
    return _first_name;
}
Mr.C64
  • 41,637
  • 14
  • 86
  • 162
2

It looks like you haven't defined your constructors for the user class.

Try changing:

user();
user(string, string, string, string, string, int) ;

to:

user() {}
user(string, string, string, string, string, int) {}

This wont cause the constructors to do anything but at least they'll exist now. I'm guessing, eventually, you'd want your second constructor to look like this:

user(string fname, string lname, string cteam, string pos, string stat, int age)
{
    setFirstName(fname);
    setLastName(lname);
    setCurrentTeam(cteam);
    setPosition(pos);
    setStatus(stat);
    setAge(age);
}
Foggzie
  • 9,691
  • 1
  • 31
  • 48
  • I'd use the initialization list syntax for data member initialization in the constructor, instead of using setters inside constructor body. Moreover, either use C++98 style of passing parameters using `const&`, or use C++11 pass-by-value and move from the value style. – Mr.C64 Jan 21 '13 at 23:47