c++ - 不断收到错误 lnk2019

标签 c++ visual-studio compiler-errors

我正在尝试创建一个基本的登录页面,但是当我尝试构建该程序时,出现语法错误。

这是我得到的错误:

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

当程序从我所能得到的调用主函数时,我得到了两次相同的错误。但是我想不出修复它的方法。

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

在此先感谢您的帮助。

最佳答案

LNK2019 不是“语法错误”。这是链接器发出的诊断。这意味着某些已声明和使用的函数(在本例中为 ReadData)尚未定义,或者如果已定义,则尚未编译,或者如果已编译,则尚未传递给链接器。

ReadData 的前向声明与实现不匹配。

避免这种情况的一个简单方法是不使用前向声明。前向声明在语言中是有原因的(支持递归,支持单独编译),但作为一般约定,它们没有优势并且确实存在一些问题,包括额外的维护工作和您现在遇到的问题。而是将每个函数定义放在首次使用之前。

关于c++ - 不断收到错误 lnk2019,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23258234/

相关文章:

c++如何输出我在函数中操作的数组?

c# - 为什么将项目添加到解决方案而不是文件夹?

compiler-errors - 在Mac上编译fortran文件时出错

c - 错误 : invalid operands to binary == (have 'struct demo' and 'struct demo' )

intellij-idea - 在 IntelliJ 上消除 Java 9 拆分包错误

c++ - 找不到匹配的重载函数 C++ TEMPLATE

java - 错误 : conflicting types for JNI method

c++ - 是否存在目标缓冲区需要多个内部字符空间的 codecvt 解码方案?

c++ - 分配控制台();与 GetAsyncKeyState();

c++ - 如何在VS2013中使用内联版本的memcpy?