我正在尝试创建基本的登录页面,但是当我尝试构建程序时,出现语法错误。
这是我得到的错误:
Error 1 error LNK2019: unresolved external symbol "struct UserRecord __cdecl ReadData(class
std::basic_ifstream<char,struct std::char_traits<char> > &,struct UserRecord)" (?ReadData@@YA?
AUUserRecord@@AAV?$basic_ifstream@DU?$char_traits@D@std@@@std@@U1@@Z) referenced in function _main
c:\Users\Emerson\documents\visual studio
2012\Projects\ConsoleApplication11\ConsoleApplication11\small_eas12d_p6.obj ConsoleApplication11
当程序从我可以得到的函数调用main函数时,我得到两次相同的错误。但是我无法找到解决问题的方法。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int Max_Persons = 50, Max_Attempts = 3;
struct UserRecord
{
string userID;
string password;
int PIN;
};
typedef struct UserRecord AccountInfo;
void PrintHeading();
void Login_Success();
void Kicked();
void Login_Failed(int);
void FileCheck(ifstream &);
UserRecord ReadData(ifstream &, AccountInfo);
bool checkData(bool, AccountInfo, int);
int main()
{
int i = 0;
bool passorfail = false;
int attempts = 0;
ifstream infile;
AccountInfo Accounts[Max_Persons];
FileCheck(infile);
Accounts[Max_Persons] = ReadData(infile, Accounts[Max_Persons]);
PrintHeading();
do
{
passorfail = checkData(passorfail, Accounts[Max_Persons], attempts);
} while (!passorfail || attempts < 3);
if (attempts <= 3)
Kicked();
if(passorfail)
Login_Success();
infile.close();
return 0;
}
AccountInfo ReadData(ifstream & infile, AccountInfo Accounts[Max_Persons])
{
int UserNum;
string str;
getline(infile, str, '\n' );
UserNum = atoi(str.c_str());
for(int i = 0; i < UserNum; i++)
{
infile >> Accounts[i].userID;
infile >> Accounts[i].password;
infile >> Accounts[i].PIN;
}
return Accounts[Max_Persons];
}
void FileCheck(ifstream & infile)
{
string FileName;
cout << "Filenames must not contain blanks ." << endl;
cout << "Please enter the file name to decode ->" << endl;
cin >> FileName;
infile.open(FileName.c_str());
while(!infile)
{
FileName.clear();
cout << "Sorry \"" << FileName << "\" is not a valid file name. Please try again." << endl;
cin >> FileName;
infile.open(FileName.c_str());
}
return;
}
bool checkData(bool passorfail, AccountInfo Accounts[Max_Persons], int attempts)
{
string givenID;
string givenPass;
int givenPin;
do
{
cout << "Login" << endl;
cout << "UserID: ";
cin >> givenID;
cout << endl << "Password: ";
cin >> givenPass;
cout << endl << "Pin: ";
cin >> givenPin;
for(int i = 0; i < Max_Persons; i++)
{
if(givenID == Accounts[i].userID)
{
if (givenPass == Accounts[i].password)
{
if (givenPin == Accounts[i].PIN)
{
passorfail = true;
}
}
}
}
if(!passorfail)
{
Login_Failed(attempts);
attempts++;
}
} while (!passorfail || attempts < 3);
return passorfail;
}
void PrintHeading()
{
cout << "-----------------------------------------------------------\n";
cout << "------ Welcome to MyGreatWebsite.com ------\n";
cout << "-----------------------------------------------------------\n";
cout << "**Unauthorized access to this stie is strictly prohibited**";
return;
}
void Login_Failed(int attempts)
{
cout << "Sorry, that username and/or password was incorrect" << endl;
cout << "You have used " << attempts << " of your attempts to login. " << endl;
cout << "You only have 3 attempts total before you are automatically " << endl;
cout << "kicked from the system" << endl;
return;
}
void Kicked()
{
cout << "Sorry, you have reached the maximum number attempts " << endl;
cout << "to access this site, please have a good day" << endl;
return;
}
void Login_Success()
{
cout << "Welcome to MyGeatWebsite.com" << endl;
cout << "Enjoy your stay" << endl;
return;
}
提前感谢您的帮助。
答案 0 :(得分:2)
LNK2019不是“语法错误”。它是链接器发出的诊断。这意味着某些已声明和使用的函数(在本例中为ReadData
)尚未定义,或者如果已定义,尚未编译,或者已编译,尚未传递给链接器。
ReadData
的前瞻声明与实施不符。
避免这种情况的一种简单方法是不使用前向声明。前向声明在语言中是有原因的(支持递归,并支持单独的编译),但作为一般约定,它们没有优势并且确实存在一些问题,包括额外的维护工作和您现在遇到的问题。而是将每个函数定义放在首次使用之前。
答案 1 :(得分:1)
只是为了澄清,在Visual Studio上,以LNK
开头的错误是Linker错误。
现在出于此错误的原因,您似乎已将ReadData
原型声明为
UserRecord ReadData(ifstream &, AccountInfo);
在函数定义中它是:
AccountInfo ReadData(ifstream & infile, AccountInfo Accounts[Max_Persons]) { }
链接器可能假定有两个不同的函数,一个接收单个AccountInfo
,另一个接收AccountInfo[]
数组。更改ReadData
的原型以匹配函数定义,它应该解决问题。