字符串和类错误 [英] string and class error

查看:77
本文介绍了字符串和类错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个类,其中包含基于帐户的

程序所需的所有信息,如下所示。当编译

字符串word时(在函数writetofile中)在

构造函数中初始化显然是未声明的,但它已在

中被声明为构造函数。

i我是c ++类编程的新手,我还需要声明它,因为

和初始化一样吗?如果没有,问题是什么?怎么能解决?
解决了?


谢谢


wilson


#include< iostream>

#include< fstream>

#include< time.h>

#include< cstdlib>

#include< cstring>

使用命名空间std;

class Account

{

friend void new_account();

public:

Account(){string word =" hello" ;; } //最终可以是用户

输入

void returnbalance(){std :: cout<<余额<<

std :: endl; }

void writetofile()

{

string extension =" .txt" ;;

ofstream writetofile;

writetofile.open((word + extension).c_str());

writetofile<<余额;

}

无效存款(浮动金额)

{

余额=余额+金额;

std :: cout<< " $" <<金额<< "已存入

到您的账户中;

std :: cout<< std :: endl<< 你的余额现在是$ <<

余额;

}

虚拟撤回(浮动金额)

{

if(金额<余额)

{

余额=余额 - 金额;

std :: cout<< ; " $" <<金额<< 已从您的帐户中提取

;

std :: cout<< std :: endl<< 你的余额是

现在$ <<余额;

}

其他

{

std :: cout<< std :: endl<< 资金不足,

您的余额为$ <<余额;

}

}

浮动余额;


私人:

int pin_number;

int account_number;

};

解决方案

< BLOCKQUOTE>" <<金额<< "已存入

到您的账户中;

std :: cout<< std :: endl<< 您的余额现在是


" <<

余额;

}

虚拟撤回(浮动金额)

{

if(金额<余额)

{

余额=余额 - 金额;

std :: cout<< ; "

" <<金额<< 已从您的帐户中提取

;

std :: cout<< std :: endl<< 您的余额是

now


i have created a class which contains all the information needed for a
program based on accounts, this is shown below. When compiled the
string "word" (in function writetofile) which is initialised in the
constructor is apparently "undeclared", yet it has been declared in
the constructor.

i am new to c++ class programming, do i need to also declare it, as
well as initialise? and if not, what is the problem and how can it be
solved?

thanks

wilson

#include <iostream>
#include <fstream>
#include <time.h>
#include <cstdlib>
#include <cstring>
using namespace std;
class Account
{
friend void new_account();
public:
Account(){string word = "hello";}//can eventually be user
inputted
void returnbalance() { std::cout << balance <<
std::endl; }
void writetofile()
{
string extension = ".txt";
ofstream writetofile;
writetofile.open((word + extension).c_str());
writetofile << balance;
}
void deposit(float amount)
{
balance = balance + amount;
std::cout << "$" << amount << " Has been deposited
into your account";
std::cout << std::endl << "Your balance is now $" <<
balance;
}
virtual void withdraw(float amount)
{
if(amount < balance)
{
balance = balance - amount;
std::cout << "$" << amount << "has been
withdrawn from your account";
std::cout << std::endl << "Your balance is
now $" << balance;
}
else
{
std::cout << std::endl << "Insufficient funds,
your balance is $" << balance;
}
}
float balance;

private:
int pin_number;
int account_number;
};

解决方案

" << amount << " Has been deposited
into your account";
std::cout << std::endl << "Your balance is now


" <<
balance;
}
virtual void withdraw(float amount)
{
if(amount < balance)
{
balance = balance - amount;
std::cout << "


" << amount << "has been
withdrawn from your account";
std::cout << std::endl << "Your balance is
now


这篇关于字符串和类错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆