This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?

(34个答案)


7年前关闭。




我正在尝试编译一个程序,以确保所有代码都能正常工作。我收到错误2019 Unresolved External并显示以下消息:
1>------ Build started: Project: Week 2 Dating Service, Configuration: Debug Win32 ------
1>  Client.cpp
1>Client.obj : error LNK2019: unresolved external symbol "private: void __thiscall Client::checkName(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?checkName@Client@@AAEXAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: __thiscall Client::Client(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0Client@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
1>Client.obj : error LNK2019: unresolved external symbol "private: void __thiscall Client::checkSex(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?checkSex@Client@@AAEXAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: __thiscall Client::Client(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0Client@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z)
1>Client.obj : error LNK2019: unresolved external symbol "private: void __thiscall Client::checkPhone(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?checkPhone@Client@@AAEXAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: __thiscall Client::Client(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0Client@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@00@Z)
1>C:\Users\owner\Documents\Visual Studio 2010\Projects\CISS 350\Week 2 Dating Service\Debug\Week 2 Dating Service.exe : fatal error LNK1120: 3 unresolved externals

这是我所有的代码...

header :
//Client.h类的规范文件。类将保存成员数据并提供访问数据所需的功能。
#ifndef CLIENT_H
#define CLIENT_H
#include <iostream>
#include <vector>

using namespace std;

class Client
{
private:
//Member Variables
string name;    //Hold the clients name
string sex; //Single char to hold sex
string phone; //Holds 7 digit phone number
vector<string> interests;

//Private Member functions
void interest(vector<string>);
void checkName(string &);
void checkSex(string &);
void checkPhone(string &);

public:
//Public Variables
bool hasMatch;
string match;

//Constructors
Client();
Client(string);
Client(string, string);
Client(string, string, string);
Client(string, string, string, vector<string>);

//Mutators
void setName(string);
void setSex(string);
void setPhone(string);
void setInterests(vector<string>);
void setClient(string, string, string, vector<string>);


//Accessors
void getName(string &);
void getSex(string &);
void getPhone(string &);
void getInterests(vector<string> &);
void getClient(string &, string &, string &, vector<string> &);

};


#endif

实现文件:
//Implementation file for the Client.h class.

#include <iostream>
#include <string>
#include <vector>
#include "Client.h"

using namespace std;

//Private Member functions
void Client::interest(vector<string> inter)
{
int vecSize = inter.size();
for(int i = 0; i < vecSize; i++)
{
    interests[i] = inter[i];
}
}
void checkName(string &nm)
{
do
{
    cout << "Name is longer than 20 characters. Please enter a new name:";
    cin >> nm;
}
while(nm.size() > 20);
}
void checkSex(string &sx)
{
int size = sx.size();

if(size > 1)
{
    sx = sx[0];
}

do
{
    if(sx != "M" || sx != "m" || sx != "F" || sx != "f")
    {
        cout << "Please enter sex(M or F):";
        cin >> sx;
    }
}
while(sx != "M" || sx != "m" || sx != "F" || sx != "f");
}
void checkPhone(string &ph)
{
int size = ph.size();
bool isGood= true;

if(size > 8)
    isGood = false;

for(int i = 0; i < size; i++)
{
    if(static_cast<int>(ph[i]) != 32 && ph[i] > '9'|| ph[i] < '0')
    {
        isGood = false;
    }
}

if(isGood == false)
{
    cout << "Phone number not valid.\n" << "Please enter a new number:";
    cin >>ph;
    checkPhone(ph);
}
}

//Contructors
Client::Client()
{
name = " ";
phone = " ";
sex = " ";
interests[0] = " ";
hasMatch = false;
match = " ";
}

Client::Client(string nm)
{
checkName(nm);
name = nm;
phone = " ";
sex = " ";
interests[0] = " ";
hasMatch = false;
match = " ";
}

Client::Client(string nm, string sx)
{
checkName(nm);
name = nm;
phone = " ";
checkSex(sx);
sex = sx;
interests[0] = " ";
hasMatch = false;
match = " ";
}

Client::Client(string nm, string sx, string ph)
{
checkName(nm);
name = nm;
checkPhone(ph);
phone = ph;
checkSex(sx);
sex = sx;
interests[0] = " ";
hasMatch = false;
match = " ";
}

Client::Client(string nm, string sx, string ph, vector<string> inter)
{
checkName(nm);
name = nm;
checkPhone(ph);
phone = ph;
checkSex(sx);
sex = sx;
interest(inter);
hasMatch = false;
match = " ";
}

//Mutators
void Client::setName(string nm)
{
checkName(nm);
name = nm;
}
void Client::setSex(string sx)
{
checkSex(sx);
sex = sx;
}
void Client::setPhone(string ph)
{
checkPhone(ph);
phone = ph;
}
void Client::setInterests(vector<string> inter)
{
interest(inter);
}
void Client::setClient(string nm, string sx, string ph, vector<string> inter)
{
checkName(nm);
name = nm;
checkPhone(ph);
phone = ph;
checkSex(sx);
sex = sx;
interest(inter);
}

//Accessors
void Client::getName(string &nm)
{
nm = name;
}
void Client::getSex(string &sx)
{
sx = sex;
}
void Client::getPhone(string &ph)
{
ph = phone;
}
void Client::getInterests(vector<string> &inter)
{
inter = interests;
}
void Client::getClient(string &nm, string &sx, string &ph, vector<string> &inter)
{
nm = name;
sx = sex;
ph = phone;
inter = interests;
}

主文件:
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <list>
#include "Client.h"

using namespace std;

//Global Variables


//Function Prototypes
char MainMenu();
void AddMenu();
void PrintMenu();
bool InitLoad();
void closeProgram();


int main()
{
list<Client> males;
list<Client> females;
Client mClient;
Client fClient;

char choice;

do
{
choice = MainMenu();


switch(choice)      //Switch to to call either user selection
    {
    case ('1'):
        {
            AddMenu();
            break;
        }
    case ('2'):
        {
            PrintMenu();
            break;
        }
    case ('3'):
        {
            closeProgram();
        }
    }
}
while(choice <= '3' || choice >= '1' || isalpha(choice));       //input      validation to ensure the user selects an acceptable value
}

char MainMenu()
{
char choice;    //For main menu choice
cout << endl;
do
{
cout << "Main Menu\n" << "1. Client Menu\n" << "2. Print Menu\n"
        << "3. Exit\n";
    cout << "Selection: ";
    cin >> choice;
    cout << endl;
}
while(choice < '1' || choice > '3' || isalpha(choice));     //input validation to ensure the user selects an acceptable value

return choice;
}
void AddMenu()
{
char choice;    //For main menu choice
cout << endl;
do
{
cout << "Client Menu\n" << "1. Add Client\n" << "2. UnMatch Client\n"
        << "3. Match Client\n" << "4. Return\n";
    cout << "Selection: ";
    cin >> choice;
    cout << endl;
}
while(choice < '1' || choice > '4' || isalpha(choice));     //input validation to ensure the user selects an acceptable value
 }
void PrintMenu()
{
char choice;    //For main menu choice
cout << endl;
do
{
cout << "Print Menu\n" << "1. Print Matched Clients\n" << "2. Print Free Clients\n"
        << "3. Print All Clients\n" << "4. Return\n";
    cout << "Selection: ";
    cin >> choice;
    cout << endl;
}
while(choice < '1' || choice > '4' || isalpha(choice));     //input validation to ensure the user selects an acceptable value
}
bool InitLoad()
{
return true;
}
void closeProgram()
{

}

最佳答案

您尚未限定许多函数定义。例如:

void checkName(string &nm)

应该是:
void Client::checkName(string &nm)

因为它是Client类的成员。 checkSexcheckPhone也是如此。

关于c++ - 编译器错误2019年 Unresolved external symbol ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15842931/

10-10 07:45