By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,277 Members | 1,235 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,277 IT Pros & Developers. It's quick & easy.

Using string variables inside classes

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.