Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / May 2007

Tip: Looking for answers? Try searching our database.

Using string variables inside classes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andy - 10 May 2007 11:15 GMT
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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.