Archived

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

Hungry Joe

#include not working in c++

Recommended Posts

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
#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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Any C++ library you release should be in its own namespace, especially if you define common things like containers or debug routines in the library. Name collision is a really stupid reason for a library to not work with a project.

I like pie.

Share this post


Link to post
Share on other sites