возникают проблемы с аутентификацией, если я использую оператор if

Существует ошибка, когда я пытаюсь скомпилировать, и я не уверен, что с ним не так.
Это программа, которая аутентифицирует имя пользователя и пароль с помощью текстового файла, разделенного символом «;» разделитель в одном текстовом файле.
Ошибка довольно длинная.

/tmp/ccgs7RYV.o: В функции 'Employee :: Employee ()':
main2.cpp :(. text + 0xa5): неопределенная ссылка на 'Employee :: authenticate (std :: basic_string, std :: allocator>, std :: basic_string, std :: allocator>)'
/tmp/ccgs7RYV.o: В функции `Employee :: Employee () ':
main2.cpp :(. text + 0x231): неопределенная ссылка на 'Employee :: authenticate (std :: basic_string, std :: allocator>, std :: basic_string, std :: allocator>)'
collect2: ld вернул 1 статус выхода
#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

Решение

bool Employee::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;
}

Вам необходимо использовать оператор разрешения области. Вы просто скучали по этому.

4

Другие решения

Хорошо, я попытаюсь немного разобраться в дизайне классов.

class Employee
{
public:
Employee( std::string name, std::string password ) :
m_name( name ), m_password( password )
{
}

bool authenticate( const char * filename ) const;

private:
std::string m_name;
std::string m_password;
};

Employee readEmployeeFromConsole()
{
std::string name, password;
std::cout << "Name: ";
std::cin >> name;
std::cout << "Password: "std::cin >> password;
return Employee( name, password );
}

bool Employee::authenticate( const char * filename ) const
{
// your implementation
}

int main()
{
Employee emp = readEmployeeFromConsole();
if( emp.authenticate( "input.txt" ) )
{
std::cout << "You're in!\n";
}
else
{
std::cout << "Get out!\n";
}
}
1

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector