Hello! I'm trying to create a header file for my upcoming project. This is my first time actually using classes so I'm not quite clear on all the do's and don't's of header files and classes. My teacher said it was alright to define the private functions, setters, and getters in the header file, but when I went to do that, I had a recurring error in my getter functions. They're supposed to return the private variables and that's it, but having the function do just that returns errors. In line 14 I tried a solution that removed the errors based off a stack overflow page where they added an underscore before the variable name. I'm worried, however, that if I do this, then when I use the function in my .cpp, it will expect me to provide that variable, when it already exists in the class and all I'm doing is changing it with the setter and retrieving its data with the setter. Any clarification on this matter would be much appreciated!
You are confusing = and ==.
= is assignment
== checks for equality
1 2 3 4
//Returns the stored username
std::string getUsername(std::string _username){
return username;
};
It's not an error but you aren't using your _username variable.
1 2 3 4
//Sets encryptedPass to the encrypted input
void setEncryptedPassword(std::string input){
encryptedPass = input;
};
I don't see the point of this function. Why let the user directly set the value of encryptedPass? The value of encryptedPass is set inside setNewPassword already.
Why have a virtual destructor if you aren't using any sort of polymorphism, by the looks of it?
Line 64: You are potentially going out of bounds if password is an empty string.
Most places I'm using == I'm using it in conjunction with the bool output of meetsCriteria to determine something. The virtual destructor was put there by the IDE when I created the header file so I will staunchly not touch it for fear something breaks. My question is what I can do to transform my getter functions so that they a) don't have compile errors and b) return their respective private variable, either username or encryptedPass.
#ifndef PASSWORDMANAGER_H
#define PASSWORDMANAGER_H
#include <string>
#include <ctype.h> // <--- Use <cctype>.
class PasswordManager
{
public:
PasswordManager();
//Sets username to input
void setUsername(std::string input)
{
username = input;
}; // <--- What is this for? You do not need it. I do not think it makes any difference, but it is not necessary.
//Returns the stored username
std::string getUsername(std::string _username) // <--- Get functions do not normally use any parameters.
{
return username;
}; // <--- What is this for? You do not need it.
//Sets encryptedPass to the encrypted input
void setEncryptedPassword(std::string input)
{
encryptedPass = input;
}; // <--- What is this for? You do not need it.
//Returns the stored EncryptedPass
std::string getEncryptedPass // <--- Be consistent with your {}s.
{
return encryptedPass;
}; // <--- What is this for? You do not need it.
//Takes a new proposed password, checks if it meets criteria, and then encrypts the password and stores it in the member variable
bool setNewPassword(std::string input)
{
if (meetsCriteria(input) == true)
{
encryptedPass = encrypt(input);
returntrue;
}
elsereturnfalse;
}; // <--- What is this for? You do not need it.
bool authenticate(std::string input)
{
std::string temp = encrypt(input);
if (encryptedPass == temp)
returntrue;
elsereturnfalse;
}
}; // <--- This closes the class. What follows is not part of the class.virtual ~PasswordManager();
private:
std::string username, encryptedPass;
//Encrypts the password, returns encrypted password
std::string encrypt(std::string password)
{
std::string output = password;
for (int i = 0; i < password.size(); i++)
output[i] = ((password[i] - 33) + 45) % 94 + 33;
return output;
}
//If the password doesn't meet criteria, returns false
bool meetsCriteria(std::string password)
{
bool criteriaMet = true, check = false;
if (password.length() < 9)
criteriaMet = false;
for (int i = 0; i < password.size(); i++)
{
if (islower(password[i]) != 0)
check = true;
}
if (check = false) // <--- This is a set not a compare.
criteriaMet = false;
if (password[0] = '_') // <--- This sets password[0] to '_'. is that what you want?
criteriaMet = false;
return criteriaMet;
}
#endif // PASSWORDMANAGER_H
In the "getUsername" function you are sending the function a variable, but never use it and the variable that you do use should produce a compiler error that the variable is undefined. See line 52.
Hey Andy. I cleaned up everything you mentioned. The getUsername function was my attempt at solving the error codes, which I forgot to add. Here they are now:
Lines 16,24 error: expected ';' at end of member declaration
Lines 15,23 error: expected '}' before return
Lines 15,23 error: could not convert '{<expression error>}' from '<brace-enclosed initializer list>' to 'std::string {aka std::basic_string<char>}'|
Lines 15,23 error: expected ';' before return
Lines 15,23 error: expected primary-expression before 'return'
It really doesn't like that I'm just returning an input and doing nothing else but it's my only outlet to bring those two variables to use where I need them. I can change the variables rn but if the compiler throws a fit then I have no way to output them. The project I have is very clear that the only thing these functions are supposed to do is return the private variables.
Yeah, that's exactly what worked. I guess I should be more careful about including parentheses. Thought I didn't need them if I didn't actually have a variable being input but I guess that's not the case. Thank you very much!
Just because a function does not have any parameter passed to it does not mean that you can omit the ().
The () are what makes it a function.
You have 2 for loops that go something like this: for (int i = 0; i < password.size(); i++). The ".size()" function returns a "size_t" variable type, so the loop iterator should match in type. for (size_t i = 0; i < password.size(); i++). Using an "int" will produce a compiler warning, but will not stop the program from compiling or running. It is best when the types match in a case like this.