473,772 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string and class error

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(){strin g word = "hello";}//can eventually be user
inputted
void returnbalance() { std::cout << balance <<
std::endl; }
void writetofile()
{
string extension = ".txt";
ofstream writetofile;
writetofile.ope n((word + extension).c_st r());
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 << "Insufficie nt funds,
your balance is $" << balance;
}
}
float balance;

private:
int pin_number;
int account_number;
};

Jun 29 '07 #1
10 1516
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
Jun 29 '07 #2
On Jun 29, 7:26 pm, John Harrison <john_androni.. .@hotmail.comwr ote:
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?

wilson

Jun 29 '07 #3
On 2007-06-29 20:21, 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.

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 <stringinstread .
<cstringis for C strings (char arrays/pointers) and using them is
generally not a good idea.

--
Erik Wikström
Jun 29 '07 #4
Wilson <tp****@googlem ail.comwrote in news:1183141269 .145871.212830
@q69g2000hsb.go oglegroups.com:
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(){strin g 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.
inputted
void returnbalance() { std::cout << balance <<
std::endl; }
void writetofile()
{
string extension = ".txt";
ofstream writetofile;
writetofile.ope n((word + extension).c_st r());
writetofile << balance;
}
Jun 29 '07 #5
Wilson <tp****@googlem ail.comwrote in news:1183142845 .258465.40950
@k29g2000hsd.go oglegroups.com:
On Jun 29, 7:26 pm, John Harrison <john_androni.. .@hotmail.comwr ote:
>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...
Jun 29 '07 #6
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.

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(){strin g word = "hello";}//can eventually be user
inputted
At this point, your compiler should bark about "string" being unknown. You
did not include the header <stringthat provides std::string but the
header <cstring>.

[snip]
Best

Kai-Uwe Bux
Jun 29 '07 #7
On Jun 29, 8:02 pm, Andre Kostur <nntps...@kostu r.netwrote:
Wilson <tpw...@googlem ail.comwrote in news:1183142845 .258465.40950
@k29g2000hsd.go oglegroups.com:


On Jun 29, 7:26 pm, John Harrison <john_androni.. .@hotmail.comwr ote:
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.ope n((word + extension).c_st r());
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 << "Insufficie nt funds,
your balance is $" << balance;
}
}
float balance;
private:
int pin_number;
int account_number;
string word = "hello";

Jun 29 '07 #8
Wilson wrote:
On Jun 29, 8:02 pm, Andre Kostur <nntps...@kostu r.netwrote:
>Wilson <tpw...@googlem ail.comwrote in news:1183142845 .258465.40950
@k29g2000hsd.g ooglegroups.com :


On Jun 29, 7:26 pm, John Harrison <john_androni.. .@hotmail.comwr ote:
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>
You have used <string>. Include it:
#include <string>
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.ope n((word + extension).c_st r());
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 << "Insufficie nt 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") {}

--
rbh
Jun 29 '07 #9
Wilson wrote:
On Jun 29, 7:26 pm, John Harrison <john_androni.. .@hotmail.comwr ote:
>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?

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
Jun 29 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
8702
by: David Carter-Hitchin | last post by:
Hi, I'm not very experienced with c++ so I'm sure this is something obvious that is easily solved, but for the life of me I can't seem to figure it out. I'd be very grateful for some knowledgeable insight into this. My class Position has a string (private, public - doesn't seem to matter which), which I construct in a member function and wish to return calls from main(). However, although
2
3284
by: Javier | last post by:
Hi All I'm in a nightmare trying to do a simple c++ program... I want to read a file that will have strings in a vector. The path where is the file is in a ini file. I did:
22
13333
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; char c; std::string str;
13
11026
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with another string. I would have thought that the code below would work... string gsub(const string & sData, const string & sFrom,
9
7135
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but when I tried the program on the Win98 system it will be running on, I get the following error: Cast from string "2076719" to type 'Long' is not valid I am not sure why I only get this error on the Win98 system or how to go about correcting...
1
3756
by: Maxwell | last post by:
Hello, I having having oodles of trouble using the std lib in my MC++ (VS.NET 2003) Class library. I figured out a simple sample to reproduce the errors I am having. Create a MC++ (VS.NET 2003) class library and type in the following code below: #include <map> #include<string>
5
7247
by: Martin Jørgensen | last post by:
Hello again, Sorry to bother but I guess my C++ book isn't very good since it obviously contains errors so the program code doesn't work with g++. However I don't understand what the problem is. Last time the problem was that "using namespace std" apparently had a "link" class/object defined already. Now I get: error: no matching function for call to 'String::String(String)'
8
1712
by: Jose Cintron | last post by:
Hello all. Newbie question here... I have a program that needs to do 1 of 2 things 1. declare a global System::String (which I think can't be done, because VS 2005 complains) 2. modify the System::String in one function An example may be easier to understand... The question is how would I get the second option to work???
9
2099
by: jerry.upstatenyguy | last post by:
I am really stuck on this. I am trying to write a string array containing a "word" and a "definition" to a class called Entry. Ultimately this will end up in another class called dictionary. No, this is not a homework assignment. In fact it was a question on my class midterm that everyone bombed. Unfortunately we never covered this in class prior to the midterm. Anyway, please help if you would be so kind. Here is what I have. I...
0
2789
by: =?Utf-8?B?Y2luZHk=?= | last post by:
I know I wrote before a week ago when I knew even less than now but I am getting better please anyone give me a clue or an example. Am I completely off track? I have a datarow in a table with the fields to map to an xsd. Then i call a web service upload method that wants the xml strongly typed to the xsd in a string. so I have generated a class from a xsd newcust.cs in .net 1.1 c# to start with just to see what it means to use an...
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7461
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4009
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2851
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.