• Announcements

Archived

This topic is now archived and is closed to further replies.

#include not working in c++

Recommended Posts

Hungry Joe    122
im kindof blundering through c++ as Im used to Java and cant seem to work out the proper way to structure classses so I keep getting all sorts of weird issues. heres the current one: this class wont compile map.h _________________________________ #ifndef __MAP_H #define __MAP_H #include <string> class CMap{ public: string test; public: CMap(){ } }; #endif _________________________________ ERRORS: error C2146: syntax error : missing '';'' before identifier ''test'' error C2501: ''CMap::string'' : missing storage-class or type specifiers error C2501: ''CMap::test'' : missing storage-class or type specifiers _________________________________ it doesnt seem to recognise string as an object identifier. Im putting all my classes (and methods) in header files (seems like Java when you do that.) as thats what teh examples in my book do. map.h is imported by the main program. why wont it compile and why should I use .cpp files?

Share on other sites
Greatwolf    125
Hmm, try using #include <string.h> see if that helps. Oh and you can put string test; and your constructor together under one public label. No need to have two separate ones.

--{You fight like a dairy farmer!}

Share on other sites
Typhoon    122
#ifndef __MAP_H
#define __MAP_H

#include <string>

class CMap{
public:
string test;
public:
CMap(){

}
};
#endif

or this

#ifndef __MAP_H
#define __MAP_H

#include <string>

class CMap{
public:
std::string test;
public:
CMap(){

}
};
#endif

You needed to use a namespace and #include won't work since it's not part of the standard.

[edited by - Typhoon on August 13, 2003 12:10:14 PM]

[edited by - Typhoon on August 13, 2003 12:11:13 PM]

Share on other sites
CloudNine    224
Make sure
using namespace std;

or
using std::string;

is declared before you include that header file.

edit: Darn!

[edited by - CloudNine on August 13, 2003 12:10:38 PM]

Share on other sites
Hungry Joe    122
thanks folks. that seemed to work (i had to put the using namespace into map.h and into the main program but it compiles.) now to find out what a namespace is!

Live forever or die in the attempt.

Share on other sites
Lektrix    106
quote:
Original post by Greatwolf
Hmm, try using #include <string.h> see if that helps.

string.h is a totally separate and different header file.

Hungry Joe: As some others have stated, you need to you use the qualified std::string.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

Share on other sites
clum    217
quote:
Original post by Hungry Joe
thanks folks. that seemed to work (i had to put the using namespace into map.h and into the main program but it compiles.) now to find out what a namespace is!

Live forever or die in the attempt.

Namespaces are like Java packages. You don''t really have to worry about them much, as long as you write using namespace std; after your includes because all standard C++ headers are in the std namespace. I''m sure you could find a tutorial about them somewhere, but I don''t think its one of C++''s most used features.

Share on other sites
Lektrix    106
quote:
Original post by clum
You don't really have to worry about them much, as long as you write using namespace std; after your includes because all standard C++ headers are in the std namespace.

Applying the using directive in this manner basically destroys the entire point of the namespaces; it merely causes the global namespace pollution to reappear.

quote:
Original post by clum
I'm sure you could find a tutorial about them somewhere, but I don't think its one of C++'s most used features.

I'd say that it is one of C++'s most used, and most useful, features. I agree that a beginner does not really need to worry about them, but I believe that they should be learned and used as they progress. They are generally taught during the scope section of a course.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on August 13, 2003 1:39:37 PM]