Using string variables inside classes

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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 <string> to 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,string,string,string);
int Add ();
};

extern AddVehicle AV;

#endif
 
Hi Andy,

Andy said:
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 <string> to 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 said:
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.
 
Back
Top