-2

I'm trying to learn using headers in C++. My sample code for the bank as follows

In following code, I just want to get a simple DepositTransaction and WithdrawTransaction class and have them inherit from base class called Transaction.

I started with the header file:

transaction.h

#ifndef TRANSACTION_H
#define TRANSACTION_H

#include <string>

class Transaction {
protected:
    std::string _customerId;
    std::string _tellerId;

public:
    Transaction(const std::string& customerId, const std::string& tellerId);

    std::string get_customer_id() const;
    std::string get_teller_id() const;

    virtual std::string get_transaction_description() const = 0;
};

class DepositTransaction : public Transaction {
private:
    double _amount;

public:
    DepositTransaction(const std::string& customerId, const std::string& tellerId, double amount);

    std::string get_transaction_description() const override;
};

class WithdrawTransaction : public Transaction {
private:
    double _amount;

public:
    WithdrawTransaction(const std::string& customerId, const std::string& tellerId, double amount);

    std::string get_transaction_description() const override;
};

#endif

Then the source file

transaction.cpp

#include "transaction.h"
#include <iostream>

Transaction::Transaction(const std::string& customerId, const std::string& tellerId)
    : _customerId(customerId), _tellerId(tellerId) {}

std::string Transaction::get_customer_id() const {
    return _customerId;
}

std::string Transaction::get_teller_id() const {
    return _tellerId;
}

DepositTransaction::DepositTransaction(const std::string& customerId, const std::string& tellerId, double amount)
    : Transaction(customerId, tellerId), _amount(amount) {}

std::string DepositTransaction::get_transaction_description() const {
    return "Deposit: " + std::to_string(_amount);
}

WithdrawTransaction::WithdrawTransaction(const std::string& customerId, const std::string& tellerId, double amount)
    : Transaction(customerId, tellerId), _amount(amount) {}

std::string WithdrawTransaction::get_transaction_description() const {
    return "Withdraw: " + std::to_string(_amount);
}

Finally, main.cpp

int main (){
    DepositTransaction deposit("12345", "Teller001", 1000);
    WithdrawTransaction withdraw("67890", "Teller002", 500);


    std::cout << deposit.get_customer_id() << std::endl;  
    std::cout << withdraw.get_teller_id() << std::endl;   

    std::cout << deposit.get_transaction_description() << std::endl;  
    std::cout << withdraw.get_transaction_description() << std::endl; 
}

But when running, I get the error

the prelaunchtask c/c++ g++ build active file terminated with exit code

Upon clicking "show errors", I get

no problems have been detected in the workspace

I tried to fix it according to this post here but no dice

  • 1
    What are the exact commands you use for compilation? Forget vscode JSON for the moment, just write the compilation commands into the terminal. – Quimby Aug 07 '23 at 14:12
  • 1
    `build active file` builds *one* file and you need to build two. – Yksisarvinen Aug 07 '23 at 14:14
  • This issue is explained in the official documentation here: [https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson](https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson) – drescherjm Aug 07 '23 at 14:19
  • [https://stackoverflow.com/a/73277441/487892](https://stackoverflow.com/a/73277441/487892) should have been the solution provided you are not using code-runner, CMakeTools or MakefileTools. If you need help with that you will need to copy and paste the exact build command from the terminal including any compiler messages. – drescherjm Aug 07 '23 at 14:22

0 Answers0