I am trying to create one vector for two types of users. Admin and Customer who are both derived from an abstract class, BaseUser. However I tried some of the answers provided online but I can't seem to make this work. I keep getting error: use of delete function 'std::unique_ptr<...
.
I am still struggling with fully grasping the concept of pointers so that could be why im stuck with this problem.
#ifndef BASEUSER_H
#define BASEUSER_H
#include <string>
class BaseUser
{
private:
int id;
int idCounter = 0;
std::string fullname;
std::string username;
std::string password;
protected:
bool isAdmin;
public:
BaseUser();
BaseUser(std::string fullname, std::string username, std::string password);
virtual void setIsAdmin(bool isAdmin) = 0;
void setID(int id);
void setFullname(std::string fullname);
void setUsername(std::string username);
void setPassword(std::string password);
unsigned long int getID();
std::string getFullname();
std::string getUsername();
std::string getPassword();
};
#endif
#ifndef ADMIN_H
#define ADMIN_H
#include "BaseUser.h"
class Admin : public BaseUser
{
public:
Admin(std::string fullname,std::string username,std::string password);
void setIsAdmin(bool isAdmin);
bool getIsAdmin();
};
#endif
#ifndef USERMANAGER_H
#define USERMANAGER_H
#include "Admin.h"
#include "Customer.h"
#include <vector>
#include <memory>
class UserManager
{
private:
std::vector<std::unique_ptr<BaseUser>> users;
bool isAuthenticated;
public:
std::vector<std::unique_ptr<BaseUser>> getUsers();
bool login(std::string name, std::string password);
bool logout();
void createAdmin(Admin);
// void createCustomer(Customer);
};
#endif
Object creation method declaration inside the usermanager class:
void UserManager::createAdmin(Admin admin))
{
users.push_back( move(admin) )
}
I also tried to push using make_unique
, but still the same error.
View that return the object to the createAdmin() method:
// View.cpp
Admin View::createAdminView()
{
string fullname, username, password;
cout << "~ Register Admin ~" << endl << endl;
cout << "Name: ";
cin.ignore();
getline(cin, fullname);
cout << "Username: ";
cin >> username;
cout << "Password: ";
cin >> password;
return Admin(fullname, username, password);
}