Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string not recognized in header file

Status
Not open for further replies.

genik

Programmer
Mar 20, 2003
3
DE
I'm just learning c++ now, so the question might be stupid, but... I didn't find any information with search or google.

I'm using MS Visual C++ 6.0. I made a new class with "Insert - New class", and I try to specify the class features. For some reason I get 3 compiler errors when trying to compile the code:

c:\dir\test\test\car.h(16) : error C2146: syntax error : missing ';' before identifier 'itsName'
c:\dir\test\test\car.h(16) : error C2501: 'string' : missing storage-class or type specifiers
c:\dir\test\test\car.h(16) : error C2501: 'itsName' : missing storage-class or type specifiers

I cannot understand why a string is not recognized. Does anybody have any idea?

The actual code is here:

// car.h: interface for the car class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_CAR_H__7D0E8056_D7F6_4086_A263_41FD17B8F1BB__INCLUDED_)
#define AFX_CAR_H__7D0E8056_D7F6_4086_A263_41FD17B8F1BB__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class car
{
public:
car();
string itsName;
int itsDoors;
int itsCylinders;
int itsEnginesize;
char itsName;
virtual ~car();
};

#endif // !defined(AFX_CAR_H__7D0E8056_D7F6_4086_A263_41FD17B8F1BB__INCLUDED_)

 
>> I'm just learning c++ now

How? Using what? A book? An online source?

Your not going to learn C/C++ very easily if your trying to piece information together off the internet. You should probably have a book for beginners on C/C++. If you have one that is any good at all it will walk you through learning about "types" etc.

c:\dir\test\test\car.h(16) : error C2146: syntax error : missing ';' before identifier 'itsName'

public:
car();
string[/red] itsName;

There is no type "string". There are other errors as well but posting these errors in a forum is probably not the best way to learn C/C++ as i stated earlier.

-pete
 
Thanks for the reply. I'm learning with a book called "C++ an introduction to programming" (Adams, Leestma, Nyhoff). The code which I posted was mainly created by MS VC++ 6.0, the only thing I added was

public:
car();
string itsName;
int itsDoors;
int itsCylinders;
int itsEnginesize;
char itsName;

and this was copied directly from the book. Therefore I would have assumed that it also works.

Ok, so there's no string data type, knowing that is a step forward. There are string objects anyway, right? I would be grateful to get some pointers how to specify the class containing a string object. There must be a way, I would assume.

Just out of curiosity, what were the other errors?
 
Well, I got it working now. I had tried before to add

using namespace std;
#include <string>

but it didn't work. By googling a little I found out that I have to add

#include <iostream>

(or some other include) before that. I also removed the line

char itsName;

which produced an error.
 
Ok my post is not worded correctly, sorry.

The error means there is no &quot;string&quot; type available at compile time. I did not mean to state that there was absolutly no &quot;string&quot; type, but when i re-read my post that is actually what it says. My bad.

anyway the &quot;string&quot; type is part of the STL so if your book is trying to teach you how to use it, there should be sample code that shows something like:

Code:
#include <string>

using namespace stl;

you must be missing those parts.

Again sorry for the messed up post.

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top