我一直收到这个错误,它不允许我编译。任何人都可以帮助我吗?
这是UsernamePasswordValidator.cpp
#include "UsernamePasswordValidator.h"
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
UsernamePasswordValidator::UsernamePasswordValidator()
{
myUsername = " ";
myPassword = " ";
my_ValidationFailed = true;
}
void UsernamePasswordValidator::setUsername(string username)
{
myUsername = username;
}
void UsernamePasswordValidator::setUsernameViaCString(char *username)
{
myUsername = username;
}
void UsernamePasswordValidator::setPasswordViaCString(char *password)
{
myPassword = password;
}
bool UsernamePasswordValidator::isValid()
{
int upperCase = 0, numCount = 0, lowerCase = 0;
for (int u = 0; u < myUsername.length(); u++)
{
if ( !(isalnum(myUsername.at(u))) )
{
my_ReasonWhyItFailed = "Username must contain both letters and numbers!";
return (my_ValidationFailed = false);
}
if ( isupper(myUsername.at(u)) )
{
upperCase++;
}
if ( islower(myUsername.at(u)) )
{
lowerCase++;
}
if( isdigit(myUsername.at(u)) )
{
numCount++;
}
}
if (myUsername.length() < 8)
{
my_ReasonWhyItFailed = "The username is too short!\n";
return (my_ValidationFailed = false);
}
else if (upperCase == 0)
{
my_ReasonWhyItFailed = "You need at least one Upper case letter.\n";
return (my_ValidationFailed = false);
}
else if (lowerCase == 0)
{
my_ReasonWhyItFailed = "You need at least one lowercase letter.\n";
return (my_ValidationFailed = false);
}
else if (numCount == 0)
{
my_ReasonWhyItFailed = "The username needs a number.\n";
return (my_ValidationFailed = false);
}
upperCase = lowerCase = numCount = 0;
for (int p = 0; p < myPassword.length(); p++)
{
if ( !(isalnum(myPassword.at(p))) )
{
my_ReasonWhyItFailed = "The password needs letters and numbers.";
return (my_ValidationFailed = false);
}
if ( isupper(myPassword.at(p)) )
{
upperCase++;
}
if ( islower(myPassword.at(p)) )
{
lowerCase++;
}
if( isdigit(myPassword.at(p)) )
{
numCount++;
}
}
if (myPassword.length() < 8)
{
my_ReasonWhyItFailed = "password does not contain enough characters.";
my_ValidationFailed = false;
}
else if (upperCase == 0)
{
my_ReasonWhyItFailed = "password does not contain a uppercase value.";
my_ValidationFailed = false;
}
else if (lowerCase == 0)
{
my_ReasonWhyItFailed = "password does not contain a lowercase value.";
my_ValidationFailed = false;
}
else if (numCount == 0)
{
my_ReasonWhyItFailed = "password does not contain a number.";
my_ValidationFailed = false;
}
else if (myPassword.find(myUsername) != string::npos)
{
my_ReasonWhyItFailed = "password cannot contain username.";
my_ValidationFailed = false;
}
return my_ValidationFailed;
}
string UsernamePasswordValidator::reasonForFailure()
{
return my_ReasonWhyItFailed;
}
void UsernamePasswordValidator::reset()
{
myPassword = "";
myUsername = "";
my_ValidationFailed = true;
}
这是我的UsernamePasswordValidator.h文件。
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
class UsernamePasswordValidator
{
public:
UsernamePasswordValidator();
void setUsername(string username);
void setPassword(string password);
void reset();
void setUsernameViaCString(char * username);
void setPasswordViaCString(char * password);
string reasonForFailure();
bool isValid();
private:
string myUsername, myPassword, my_ReasonWhyItFailed;
bool my_ValidationFailed;
};
最后这是我的代码驱动程序。
#include <iostream>
#include <cctype>
#include <string>
#include "UsernamePasswordValidator.h"
using namespace std;
int main()
{
UsernamePasswordValidator upv;
string user, pass;
char * badpassword = "data ";
char * goodpassword = "goodPassword";
cout << "CS52 Username/Password Validator \n";
cout << "\n Username:\n";
getline( cin, user ); // reads a whole line of input
cout << "\n Password: \n";
getline( cin, pass ); // reads a whole line of input
upv.setUsername( user );
upv.setPassword( pass );
if (upv.isValid() )
{
cout << "The Username/Password combination is valid!" << endl;
}
else
{
cout << upv.reasonForFailure( ) << endl;
}
upv.reset( );
upv.setUsername( user );
upv.setPassword( badpassword );
if (upv.isValid() )
{
cout << "The Username/Password combination is valid!" << endl;
}
else
{
cout << upv.reasonForFailure( ) << endl;
}
return 0;
}
答案 0 :(得分:0)
您已声明上述方法但未定义。 在链接期间,链接器无法找到setPassword的定义
在UsernamePasswordValidator.cpp中添加以下内容:
void UsernamePasswordValidator::setPassword(string password)
{
myPassword = password;
}