因此,由于某种原因,当我尝试编译时,我在 main.cpp 中得到一个错误:值和余额未在范围内声明。
我在 main 中使用了#include "account.h" 那么为什么没有定义它们呢?
你还看到我的类有什么问题吗,包括构造函数和析构函数。
帐号.cpp
using namespace std;
#include <iostream>
#include "account.h"
account::account(){
}
account::~account(){
}
int account::init(){
cout << "made it" << endl;
balance = value;
}
int account::deposit(){
balance = balance + value;
}
int account::withdraw(){
balance = balance - value;
}
主文件
using namespace std;
#include <iostream>
#include "account.h"
//Function for handling I/O
int accounting(){
string command;
cout << "account> ";
cin >> command;
account* c = new account;
//exits prompt
if (command == "quit"){
return 0;
}
//prints balance
else if (command == "init"){
cin >> value;
c->init();
cout << value << endl;
accounting();
}
//prints balance
else if (command == "balance"){
account* c = new account;
cout << " " << balance << endl;
accounting();
}
//deposits value
else if (command == "deposit"){
cin >> value;
c->deposit();
accounting();
}
//withdraws value
else if (command == "withdraw"){
cin >> value;
cout << "withdrawing " << value << endl;
accounting();
}
//error handling
else{
cout << "Error! Command not supported" << endl;
accounting();
}
}
int main() {
accounting();
return 0;
}
帐户.h
class account{
private:
int balance;
public:
account(); // destructor
~account(); // destructor
int value;
int deposit();
int withdraw();
int init();
};
抱歉,如果代码风格不好,我很难使用堆栈溢出编辑器。