473,320 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Using string variables inside classes

Hello,
I'm trying to use a string type variable inside a class. I can do this fine
with no problems when the class is inside a cpp file simply by adding
#include <string>.
However, my program has the class specification in a header file and the
functions in a cpp file, for better organisation and linking. When I specify
a string in the class definition in the header file I get
"syntax error : missing ';' before identifier 'avOwnerName'"
I've tried adding #include <stringto different parts of the declaration in
the header file with no luck. Is there some way to do this that I'm missing?
Thank you,
- Andy

--- Code from AddVehicle.h ---
#ifndef _ADDVEHICLE_H_
#define _ADDVEHICLE_H_

class AddVehicle
{
string avType;
string avOwnerName;
string avManufacture;
string avColour;
string avModel;
string avStolen;
string avLegal;
string avMOT;
int avAtt1, avAtt2, avAtt3;
public:
void set_values
(string,string,string,string,string,int,int,int,st ring,string,string);
int Add ();
};

extern AddVehicle AV;

#endif
May 10 '07 #1
3 1718
Hi Andy,

"Andy" <An**@discussions.microsoft.comschrieb im Newsbeitrag
news:71**********************************@microsof t.com...
Hello,
I'm trying to use a string type variable inside a class.
I can do this fine with no problems when the class is inside
a cpp file simply by adding
#include <string>.
However, my program has the class specification in a header
file and the functions in a cpp file, for better organisation
and linking. When I specify a string in the class definition
in the header file I get "syntax error : missing ';' before
identifier 'avOwnerName'"
I've tried adding #include <stringto different parts
of the declaration in the header file with no luck. Is there
some way to do this that I'm missing?
string lives in namespace std. So fix you code like below:
--- Code from AddVehicle.h ---
#ifndef _ADDVEHICLE_H_
#define _ADDVEHICLE_H_
#include <string>
class AddVehicle
{
std:string avType;
std:string avOwnerName;
// ...
public:
void set_values(std::string,std::string);
int Add ();
};
You might feel tempted to add a using namespace::std in your header but
don't do that. You will introduce std in every module which includes your
header and that might be unexpected by the module writer. using namespace
should only be used in cpp files.

--
SvenC

May 10 '07 #2
This worked perfectly, thank you very much for your speedy reply.

- Andy

"SvenC" wrote:
Hi Andy,

"Andy" <An**@discussions.microsoft.comschrieb im Newsbeitrag
news:71**********************************@microsof t.com...
Hello,
I'm trying to use a string type variable inside a class.
I can do this fine with no problems when the class is inside
a cpp file simply by adding
#include <string>.
However, my program has the class specification in a header
file and the functions in a cpp file, for better organisation
and linking. When I specify a string in the class definition
in the header file I get "syntax error : missing ';' before
identifier 'avOwnerName'"
I've tried adding #include <stringto different parts
of the declaration in the header file with no luck. Is there
some way to do this that I'm missing?

string lives in namespace std. So fix you code like below:
--- Code from AddVehicle.h ---
#ifndef _ADDVEHICLE_H_
#define _ADDVEHICLE_H_

#include <string>
class AddVehicle
{
std:string avType;
std:string avOwnerName;
// ...
public:
void set_values(std::string,std::string);
int Add ();
};

You might feel tempted to add a using namespace::std in your header but
don't do that. You will introduce std in every module which includes your
header and that might be unexpected by the module writer. using namespace
should only be used in cpp files.

--
SvenC

May 10 '07 #3
Hi Andy,

"Andy" <An**@discussions.microsoft.comschrieb im Newsbeitrag
news:6B**********************************@microsof t.com...
This worked perfectly, thank you very much for your speedy reply.
You're welcome.

--
SvenC
- Andy

"SvenC" wrote:
>Hi Andy,

"Andy" <An**@discussions.microsoft.comschrieb im Newsbeitrag
news:71**********************************@microso ft.com...
Hello,
I'm trying to use a string type variable inside a class.
I can do this fine with no problems when the class is inside
a cpp file simply by adding
#include <string>.
However, my program has the class specification in a header
file and the functions in a cpp file, for better organisation
and linking. When I specify a string in the class definition
in the header file I get "syntax error : missing ';' before
identifier 'avOwnerName'"
I've tried adding #include <stringto different parts
of the declaration in the header file with no luck. Is there
some way to do this that I'm missing?

string lives in namespace std. So fix you code like below:
--- Code from AddVehicle.h ---
#ifndef _ADDVEHICLE_H_
#define _ADDVEHICLE_H_

#include <string>
class AddVehicle
{
std:string avType;
std:string avOwnerName;
// ...
public:
void set_values(std::string,std::string);
int Add ();
};

You might feel tempted to add a using namespace::std in your header but
don't do that. You will introduce std in every module which includes your
header and that might be unexpected by the module writer. using namespace
should only be used in cpp files.

--
SvenC

May 10 '07 #4

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

Similar topics

3
by: David MacQuigg | last post by:
I am writing a chapter for teaching OOP in Python. This chapter is intended as a brief introduction to replace the more complete discussion in Learning Python, 2nd ed, pp. 295-390. I need to...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
7
by: fakeprogress | last post by:
For a homework assignment in my Data Structures/C++ class, I have to create the interface and implementation for a class called Book, create objects within the class, and process transactions that...
3
by: lars.uffmann | last post by:
Hi everyone! I am debugging a big piece of code on the search for memory leaks, using g++ under suse 9.3. Since I'm trying to eliminate ALL memory leaks, I now stumbled upon a class foo that is...
5
by: andrewmorrey | last post by:
Hello, I've got a VC++ project containing multiple classes and a main function. In one of the class functions, it reads from a text file and places the data into a vector; //...
9
by: Justin Voelker | last post by:
I have a configuration file that contains the host, username, password, and database name required for any database connections. The config file runs through a few if/then/else statements to...
14
by: raylopez99 | last post by:
KeyDown won't work KeyPress fails KeyDown not seen inspired by a poster here:http://tinyurl.com/62d97l I found some interesting stuff, which I reproduce below for newbies like me. The main...
13
by: Henri.Chinasque | last post by:
Hi all, I am wondering about thread safety and member variables. If I have such a class: class foo { private float m_floater = 0.0; public void bar(){ m_floater = true; }
15
by: r0g | last post by:
Hi There, I know you can use eval to dynamically generate the name of a function you may want to call. Can it (or some equivalent method) also be used to do the same thing for the variables of a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.