string and class error

W

Wilson

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;


};
 
J

John Harrison

Wilson said:
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
 
W

Wilson

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?

wilson
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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>

You really should bot be using <cstring>, use <string> instread.
<cstring> is for C strings (char arrays/pointers) and using them is
generally not a good idea.
 
A

Andre Kostur

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

Let's put a little whitespace in there:

Account()
{
string word = "hello";
}


That's a local variable (no different than "extension" that you use
below). It no longer exists once the constructor has completed.
 
A

Andre Kostur

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...
 
K

Kai-Uwe Bux

Wilson said:
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

At this point, your compiler should bark about "string" being unknown. You
did not include the header <string> that provides std::string but the
header <cstring>.

[snip]


Best

Kai-Uwe Bux
 
W

Wilson

@k29g2000hsd.googlegroups.com:








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";
 
R

Robert Bauck Hamar

Wilson said:
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>

You have used <string>. Include it:
#include said:
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";

You can't initialise things here. Just "string word;" will do. Initialise it
in the constructor:

Account() : word("hello") {}
 
J

John Harrison

Wilson said:
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?

wilson

Somehow I knew this was going to be the case.

You're are mixing up 'declaring' with 'assigning a value'

class Account
{
Account()
{
word = "hello"; // assign a value to word
}
string word; // declare word
};

john
 
B

BobR

Wilson said:
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.

No, you didn't! see below
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> // remove this, you are not using it.
#include <cstdlib> // remove this, you are not using it.
#include <cstring> // remove this, you are not using it.
using namespace std;
// if this is a header, remove that!!
class Account{
friend void new_account(); // ???
public:
Account(){string word = "hello";}
//can eventually be user inputted

Where did you declare/define 'string'?
Hint: #include <string>

Account( std::string mystr = "hello" ){
std::string word = mystr; // or: word( mystr );
} // 'word' disappears right here.
void returnbalance(){
std::cout << balance <<std::endl;
}

Bad name. If you say 'return', then return something.

float ReturnBalance() const { // should use 'double'
return balance;
}

// > void writetofile(){
// > string extension = ".txt";
// > ofstream writetofile;
// > writetofile.open((word + extension).c_str());

Note: 'word' is NOT defined here (it's not *in* your class).

void WriteToFile( std::string const &word ) /* const */ {
std::string filename( word );
filename += ".txt";
std::eek:fstream ofile( filename.c_str() );
if( not ofile.is_open() ){ /* error */ return;}
// > ofile << balance;

WriteTo( ofile ); // see below
WriteTo( std::cout ); // see below

void WriteTo( std::eek:stream &out ) /* const */ {
if( not out ){ /* error */ return;}
out << balance;
} // now you can 'print' to any std ostream obj.
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;
};

Don't hesitate to ask if you need more help.

[corrections always welcome.]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top