I am getting this undefined reference to `PasswordFile::addpw(UserPw)'
I am just getting back into c++ and I know it has something to do with the UserPW I am calling I believe? Any hints much appreciated. I am basically using a text file to store user name and password along with inserting new ones into the file.
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
usingnamespace std;
class UserPw
{
public:
UserPw(string user, string password) //Constructor
{
this->user = user;
this->password = password;
}//Constructor
string GetUser(); // returns the user
bool Checkpw(string user, string passwd); // returns true if user and password both match
private :
string user, password;
};
class PasswordFile
{
public:
PasswordFile(string filename);// opens the file and reads the names/passwords in the vector entry
vector<UserPw> getFile(); // returns the vector entry
void addpw(UserPw newentry); //this adds a new user/password to entry and writes entry to the file filename
bool checkpw(string user, string passwd); // returns true is user exists and password matches
private:
string filename; // the file that contains password information
vector<UserPw> entry; // the list of usernames/passwords
};
PasswordFile::PasswordFile(string filename)
{
this->filename = filename; //points to this filename
string user, password;
ifstream pfile;
pfile.open(this->filename.c_str());
pfile >> user >> password;
while (pfile.good())
{
entry.push_back(UserPw(user, password));
pfile >> user >> password;
}
} // end of PasswordFile:PasswordFile(string filename)
int main()
{
PasswordFile passfile("password.txt");
passfile.addpw(UserPw("dbotting","123qwe"));
passfile.addpw(UserPw("egomez","qwerty"));
passfile.addpw(UserPw("tongyu","liberty"));
// write some lines to see if passwords match users
}
So I got the file to accept the values for the username and password. So I am not sure how I would go about checking that the password matches the username.
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
usingnamespace std;
class UserPw
{
public:
UserPw(string user, string password); //Constructor
string GetUser(); // returns the user
string GetPassword(); // returns the password
bool Checkpw(string user, string passwd); // returns true if user and password both match
private :
string user, password;
};
UserPw::UserPw(string user, string password)
{
this-> user = user;
this-> password = password;
}
string UserPw::GetUser()
{
return user;
}
string UserPw::GetPassword()
{
return password;
}
bool UserPw::Checkpw(string user, string passwd) // add this part in need to go through it more
{
this -> user = user;
this -> passwd = passwd;
}
class PasswordFile
{
public:
PasswordFile(string filename);// opens the file and reads the names/passwords in the vector entry
vector<UserPw> getFile(); // returns the vector entry
void addpw(UserPw newentry); //this adds a new user/password to entry and writes entry to the file filename
bool checkpw(string user, string passwd); // returns true is user exists and password matches
private:
string filename; // the file that contains password information
vector<UserPw> entry; // the list of usernames/passwords
void write();
};
PasswordFile::PasswordFile(string filename)
{
this->filename = filename; //points to this filename
string user, password;
ifstream pfile;
pfile.open(this->filename.c_str());
pfile >> user >> password;
while (pfile.good())
{
entry.push_back(UserPw(user, password));
pfile >> user >> password;
}
} // end of PasswordFile:PasswordFile(string filename)
vector <UserPw> PasswordFile::getFile()
{
return getFile();
}
void PasswordFile:: addpw(UserPw newentry)
{
entry.push_back(newentry);
write();
}
void PasswordFile::write()
{
ofstream outfile;
outfile.open(filename.c_str());
for (int i = 0; i < entry.size(); i++)
{
outfile << entry[i].GetUser() << " " << entry[i].GetPassword() << endl;
}
outfile.close();
}
/*bool PasswordFile::checkpw(string user, string password)
{
}*/
int main()
{
PasswordFile passfile("password.txt");
passfile.addpw(UserPw("dbotting","123qwe"));
passfile.addpw(UserPw("egomez","qwerty"));
passfile.addpw(UserPw("tongyu","liberty"));
// write some lines to see if passwords match users
//bool checkpw(string user, string passwd);
}
To help you though the answer to a large extent is written in the function template presumable given to you as part of the assignment. Feed the strings to the function and it comes back with a bool true or false. All you have to do is check whether the two strings are equal. If they are then the answer is true otherwise the answer returned is false.
- when you get an error the error message will probably include the line number. It would certainly help. ;)
- However despite this, I'm reasonably sure it's because you haven't defined what UserPW is. I guess you mean to say something like, string UserPW = user; then int userNo = user;
With that info the function can then proceed with checking each entry in 'passfile' (hint) and returning the appropriate bool value.
However, (strong hint) you must also note that you cant refer to entry [UserPW] as it stands in your code because the variable in brackets can't be a string!
It might be better to post all I have now, instead of a section to better understand. The error I get is at line 106 if you didn't already know. What you were talking about isn't that the string UserPw:GetUser() aka line 27? Thanks again for the help so far.
For some reason it is not actually checking the passwords. I moved the function up because I thought it was closing the file before it actually checked but that was not it.. ?