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
SvenC - 10 May 2007 11:25 GMT
Hi Andy,
> Hello,
> I'm trying to use a string type variable inside a class.
[quoted text clipped - 9 lines]
> 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
Andy - 10 May 2007 11:35 GMT
This worked perfectly, thank you very much for your speedy reply.
- Andy
> Hi Andy,
>
[quoted text clipped - 37 lines]
> --
> SvenC
SvenC - 10 May 2007 11:51 GMT
Hi Andy,
> This worked perfectly, thank you very much for your speedy reply.
You're welcome.
--
SvenC
> - Andy
>
[quoted text clipped - 39 lines]
>> --
>> SvenC