-2

I have tried searching on Google, only to find nothing.

And it seems the manual also doesn't work.

Here is my code:

account.h

#include <string>
#include "Date.h"

class SavingsAccount
{
public:
    void show();   
    void deposit(Date now_date, double money, string ways);     
    void withdraw(Date now_date, double money, const char* ways);   
    void settle(Date new_date);   
    SavingsAccount(Date date_new, const char* id_new, double rate_new);
    void accountshow();
    static double getTotal();  
    static SavingsAccount* point; 
    static int sum_account;  
    static double sum_money;
private:
    const char* id;
    double balance = 0;
    double rate = 0;
    int lastDate = 0;
    Date date;
    double interest = 0;
};

account.cpp

#include <iostream>
#include <string>
#include "account.h"
#include "Date.h"
using namespace std;

double SavingsAccount::sum_money = 0;

void SavingsAccount::deposit(Date now_date, double money, string ways)   //<--- here is the problem
{
    interest += balance * (now_date - date) * rate / 366;
    date = now_date;
    date.show();
    cout << "\t#" << id << "\t" << money << "\t" << money + balance << "\t"<<ways << endl;
    balance += money;
    sum_money += money;
}

void SavingsAccount::withdraw(Date now_date, double money, const char* ways)   //取款
{
    interest += balance * (now_date - date) * rate / 366;
    date = now_date;
    date.show();
    cout << "\t#" << id << "\t-" << money << "\t" << balance - money << "\t" << ways << endl;
    balance -= money;
    sum_money -= money;
}

void SavingsAccount::settle(Date new_date)     //结算利息
{
    interest += balance * (new_date - date) * rate / 366;
    interest = (int(interest * 100 + 0.5)) / 100.0;
    sum_money += interest;
    date = new_date;
    date.show();
    cout << "\t#" << id << "\t" << interest << "\t" << interest + balance << " interest"<<endl;
    balance += interest;
    interest = 0;
}

void SavingsAccount::accountshow()
{
    this->date.show();
    cout << "\t" << "#" << id << " created" << endl;
}

void SavingsAccount::show()
{
    cout << id << "\tBalance: " << balance;
}

SavingsAccount::SavingsAccount(Date date_new, const char* id_new, double rate_new) : id(id_new)
{
    date = date_new;
    rate = rate_new;
    //sum_account++;
    balance = 0;
    interest = 0;
    accountshow();
}

double SavingsAccount::getTotal()
{
    return sum_money;
}

bank.cpp:

#include "account.h"
#include <iostream>
using namespace std;

int main()
{
    Date date(2008, 11, 1);
    SavingsAccount accounts[] = {
        SavingsAccount(date, "S3755217", 0.015),
        SavingsAccount(date, "02342342", 0.015)
    };

    const int n = sizeof(accounts) / sizeof(SavingsAccount); 
    string works = "salary";
    accounts[0].deposit(Date(2008, 11, 5), 5000, works);
    accounts[1].deposit(Date(2008, 11, 25), 10000, "sell stock 0323");
    accounts[0].deposit(Date(2008, 12, 5), 5500, "salary");
    accounts[1].withdraw(Date(2008, 12, 20), 4000, "buy a laptop");
    cout << endl;
    for (int i = 0; i < n; i++) {
        accounts[i].settle(Date(2009, 1, 1));
        accounts[i].show();
        cout << endl;
    }
    cout << "Total: " << SavingsAccount::getTotal() << endl;
    return 0;
}

Because of the limited length for this, I did not show the Date.h and Date.cpp, and there is nothing wrong with it. But I will show it to you if you need.

Here is the problem, and I translate the error the VS tells me into English:

image

VS also says:

'void SavingsAccount::deposit(Date,double,std::string)' there is no overloading member function declared in SavingsAccount

But when I replace the string by const char*, just like what I write in the withdraw() function, it works without any problem.

here is the result

So, could you please tell me how can I use string to finish this problem? Because const char* is so uncomfortable to be used.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    What problem are you looking for help with? The inability to use `std::string`? If so, reduce the code example to the minimum necessary to reproduce the problem (make a [mre]). This will allow us to reproduce the compiler diagnostics in whatever language is supported by our tools. – user4581301 Sep 13 '22 at 17:26
  • 2
    Until the first `using namespace std;` is read by the compiler, `string` is treated as an unknown type by the compiler, since it doesn't know you want to refer to the type from namespace `std`. This occurs both for `main.cpp` and `account.cpp` (assuming `Date.h` doesn't contain `using namespace std;`). Just use `std::string` instead of `string`, at least in the header (you don't make assumptions whether the user wants to everything in `std` to be available via the global namespace). – fabian Sep 13 '22 at 17:29

1 Answers1

2

In account.h, you have #include <string>, but you do not have using namespace std; (and rightly so), so the compiler doesn't know what string is. You have to use std::string instead, eg:

void deposit(Date now_date, double money, std::string ways);

Or, use using std::string, at least (not recommended in a header, though):

using std::string;
...
void deposit(Date now_date, double money, string ways);

In account.cpp and bank.cpp, you have using namespace std; after #include <string>, so the compiler can work out what string is without needing you to qualify it with std::.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thank you for your help.It works well.This is my first time to try to use multiple files to sovle a problem.The question seems a little stupid. – user19987806 Sep 14 '22 at 01:56