1
我不斷收到這個錯誤,它不會讓我編譯誰能幫我錯誤3錯誤LNK2019:無法解析的外部符號「市民:無效__thiscall
這是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;
}
與樣品定義 – Nandu
修改非常感謝你。我修好了它! –
太好了。如果它解決了你的錯誤,請接受答案 – Nandu