1

可能重复:
什么是未定义的引用/未解决的外部符号错误,我该如何解决?

我正在尝试编写大学作业并且似乎在编译时遇到问题,我已经对错误进行了 Google 搜索,并且我看到的修复程序不适用于我的代码。我会很感激你的帮助。

以下是错误代码:

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

以下是我的代码的副本:

#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() ;
    }
}

谢谢你的帮助。

4

2 回答 2

5

您没有为构造函数提供实现:

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

尝试为它们添加主体:

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)
  {
  }

....
};

请注意,为数据成员提供一些前缀很方便,以将它们与参数区分开来。一些约定以_or为前缀m_(例如name是参数,_name或者m_name是数据成员)。

另请注意,在 C++11 中(并且从您的错误代码中,我读到您正在使用 VS2010,它具有某种形式的移动语义可用),当您传递一个可移动且移动成本低的类时,您有要制作本地副本(例如设置数据成员时),您可以通过值和std::move值传递,例如

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

另请注意,吸气剂往往是const

const string& get_first_name() const
{
    return _first_name;
}
于 2013-01-21T23:44:11.547 回答
2

看起来您还没有为user该类定义构造函数。

尝试改变:

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

至:

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

这不会导致构造函数做任何事情,但至少它们现在会存在。我猜,最终,你会希望你的第二个构造函数看起来像这样:

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);
}
于 2013-01-21T23:22:08.207 回答