Re: string and class error
On Jun 29, 8:02 pm, Andre Kostur <nntps...@kostur.net> wrote:
Wilson <tpw...@googlemail.com> wrote in news:1183142845.258465.40950
@k29g2000hsd.googlegroups.com:
On Jun 29, 7:26 pm, John Harrison <john_androni...@hotmail.com> wrote:
Wilson wrote:
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.
That's the problem. It's been declared in the constructor, but you
should have declared it in the class, just like pin_number and
account_number.
If you declare something in a constructor, it is only visible in the
constructor. If you declare it in a class, it is visible thoughout the
class.
john
thanks, however i had already done this, and i was provided with the
messages "ISO C++ forbids the initialisation of member 'word'" and
"making word static". These are given when the string is declared both
as private (as is ideal, but not necesary) and public.
is there a way to solve this?
Show us the code.... we shouldn't have to guess at what you changed it
to...- Hide quoted text -
- Show quoted text -
this is what i changed the code to, after removing it from the
constructor. i am told that ISO C++ forbids the initialisation of
"word", why is this? can it be solved?
#include <iostream>
#include <fstream>
#include <time.h>
#include <cstdlib>
#include <cstring>
using namespace std;
class Account
{
friend void new_account();
public:
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;
string word = "hello";