I'm working on a program that insert, delete accounts from a Bank.
Here is my .hpp code :
#ifndef DEF_BANK
#define DEF_BANK
#include <iostream>
using namespace std;
class Bank
{
private:
class _Account
{
public:
_Account(string, float);
string getClient();
float getBalance();
_Account *getNext();
void setClient(string);
void setBalance(float);
void setNext(Bank::_Account *);
private:
string _client; //nom client
float _balance; // stocke balance du compte
_Account *_next; // next account
};
_Account *_head;
public:
Bank();
Bank(string name, float balance);
_Account *rechercheClient(string);
float withdraw(string, float);
float deposit(string, float);
void createAccount(string, float);
void insert(string, float);
void remove(string name);
float deleteAccount(string);
void mergeAccounts(string, string);
void displayAccounts();
};
#endif
And here is my .cpp insert function:
void Bank::insert(string name, float balance)
{
_Account *temp(_head);
//_Account *n = new _Account(name, balance);
bool flag(true);
while(temp)
{
if (temp->getClient() == name)
{
/* code */
cout << "DENIED OPERATION! --> "<< name <<"’s account already exists." << endl;
flag = false;
}
temp = temp->getNext();
}
if (flag)
{
/* code */
temp->setNext(new _Account(name, balance));
}
}
Why when I try this in main.cpp:
int main()
{
Bank account_1;
account_1.insert("Hamza", 1000.0);
}
I get a segmentation fault :11 ? because I don't see my fault in the code.