mercredi 15 décembre 2021

While I was learning Inheritance in C++ 17...I came to an error as shown.. [ Error: non-standard syntax; use '&' to create a pointer to member ]

Error : after I tried to run my project

Elaboration: Was trying to learn Inheritance ,by deriving a "Savings Account" class from the parent class => "Account" class. But now I am stuck. Error: non-standard syntax; use '&' to create a pointer to member

main.cpp

#include <iostream>
#include <string>
#include "Account.h"
#include "Savings_Account.h"

using namespace std;

int main() {

    cout << "\nAccount=======================" << endl;
    Account acc{};
    acc.deposit(2000.0);
    acc.withdraw(500.0);

    cout << endl;

    Account* p_acc{ nullptr };
    p_acc = new Account();
    p_acc->deposit(1000.0);
    p_acc->withdraw(500.0);
    delete p_acc;

    cout << "\nSavings_Account===============" << endl;
    Savings_Account sav_acc{};
    sav_acc.deposit(2000.0);
    sav_acc.withdraw(500.0);

    cout << endl;

    Savings_Account* p_sav_acc{ nullptr };
    p_sav_acc = new Savings_Account();
    p_sav_acc->deposit(1000);
    p_sav_acc->withdraw(500.0);
    delete p_sav_acc;

    return 0;
}

Account.h

#ifndef _ACCOUNT_H_
#define _ACCOUNT_H_
#include <iostream>
#include <string>

using namespace std;

class Account {
public:
    double balance;
    string name;
    void deposit(double amount);
    void withdraw(double amount);
    Account();
    ~Account();
};

#endif

Account.cpp

#define _CRT_SECURE_NO_WANINGS
#include "Account.h"

Account::Account() 
    : balance{ 0 }, name{ "NULL" }{

}

Account::~Account() {

}

void Account::deposit(double amount) {
    cout << "Account deposit was " << amount << endl;
}

void Account::withdraw(double amount) {
    cout << "Account withdraw was " << amount << endl;
}

Savings_Account.h

#ifndef _SAVINGS_ACCOUNT_H_
#define _SAVINGS_ACCOUNT_H_
#include <iostream>
#include <string>
#include "Account.h"

class Savings_Account : public Account {
public:
    double interest_rate;
    Savings_Account();
    ~Savings_Account();
    void deposit(double amount);
    void withdraw(double amount);
};

#endif

Savings_Account.cpp

#define _CRT_SECURE_NO_WANINGS
#include "Savings_Account.h"

Savings_Account::Savings_Account()
    :interest_rate{ 3.0 } {

}

Savings_Account::~Savings_Account() {

}

void Savings_Account::deposit(double amount) {
    cout << "Savings Account deposited " << deposit << endl;
}

void Savings_Account::withdraw(double amount) {
    cout << "Savings Account withdraw " << withdraw << endl;
}

Note : Using Visual Studio-2022 IDE.

Aucun commentaire:

Enregistrer un commentaire