473,770 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,s tring,int,int,i nt,string,strin g,string);
int Add ();
};

extern AddVehicle AV;

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

"Andy" <An**@discussio ns.microsoft.co mschrieb im Newsbeitrag
news:71******** *************** ***********@mic rosoft.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::st ring);
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**@discussio ns.microsoft.co mschrieb im Newsbeitrag
news:71******** *************** ***********@mic rosoft.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::st ring);
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**@discussio ns.microsoft.co mschrieb im Newsbeitrag
news:6B******** *************** ***********@mic rosoft.com...
This worked perfectly, thank you very much for your speedy reply.
You're welcome.

--
SvenC
- Andy

"SvenC" wrote:
>Hi Andy,

"Andy" <An**@discussio ns.microsoft.co mschrieb im Newsbeitrag
news:71******* *************** ************@mi crosoft.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::st ring);
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
2527
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 explain instance variables. What I'm looking for is the best compromise between brevity and a full explanation. The students are non-CIS technical professionals ( engineers and scientists ). At the point they need this explanation, they have...
28
20342
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()', this.pinginterval); - but is there no way to do this without using the literal ObjectName? If I write 'this.methodName()' I get "Line 1 Char 1: Object doesn't support this property or method." in IE, and nothing happens in Firebird.
7
2958
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 manipulate (and report on) members of the class. Interface consists of: - 5 private variables char author; char title; char code;
3
1844
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 not ever instantiated (thus no constructor or destructor ever called), but instead, all of its member variables are defined as static in the according .cpp file, and it's methods are invoked by calling foo::bar(...) to invoke method "bar(...)".
5
2160
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; // std::vector<std::vector<std::string applications (50, std::vector<std::string>(12)); applications = "Test1"; applications = "Test2";
9
2828
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 determine if the website is local or remote and if it is local if it is the test site or "live" site then it sets the 4 variables. I am trying to implement a webstats package that has it's own config file. I would the new config file to be able to use...
14
2974
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 reason you would want to do this is for example to trigger something from an OnPaint event without resorting to boolean switches-- say if a user presses the "M" key while the program is Painting, the user gets the PaintHandler to do something else. ...
13
11478
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
1791
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 class e.g. class Foo(): bar = 1 gum = 2
0
9618
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10259
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9906
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.