有一个错误,当我尝试编译和即时消息不知道它有什么问题。 这是一个使用文本文件来验证用户名和密码的程序,用“;”分隔一个文本文件中的分隔符。 错误很长。有身份验证问题,如果我使用if语句
/tmp/ccgs7RYV.o: In function 'Employee::Employee()': main2.cpp:(.text+0xa5): undefined reference to 'Employee::authenticate(std::basic_string, std::allocator>, std::basic_string, std::allocator>)' /tmp/ccgs7RYV.o: In function `Employee::Employee()': main2.cpp:(.text+0x231): undefined reference to 'Employee::authenticate(std::basic_string, std::allocator>, std::basic_string, std::allocator>)' collect2: ld returned 1 exit status
#include<iostream>
#include<string>
#include <fstream>
using namespace std;
class Employee
{
public:
Employee();
bool authenticate(string, string);
};
Employee::Employee()
{
string username, password;
cout << "Username: ";
cin >> username;
cout << "Password: ";
cin >> password;
if (authenticate(username, password) == true)
cout << "Sucess" << endl;
else
cout << "fail" << endl;
}
bool authenticate(string username, string password)
{
std::ifstream file("login.txt");
std::string fusername, fpassword;
while (!file.fail())
{
std::getline(file, fusername, ';'); // use ; as delimiter
std::getline(file, fpassword); // use line end as delimiter
// remember - delimiter readed from input but not added to output
if (fusername == username && fpassword == password)
return true;
}
return false;
}
int main()
{
Employee();
return 0;
}
请1)停止使用[功课]标签,它说在说明停止使用2)开始与你在编写编程语言标记您的问题。 – BoltClock
你while循环已基本破坏。你不需要'fail()',但是你必须检查'getline'的返回值。 –
类并不是真的被设计为像构造函数中的所有逻辑一样被作为临时对象调用。 为什么不把Employee作为一个对象,然后给它一个方法进行验证,该方法需要输入文件? – CashCow