By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,028 Members | 1,010 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,028 IT Pros & Developers. It's quick & easy.

Collection class helper objects in classes?

P: n/a
Hi,

Is is possible to use a collection class instance as a helper object in a class?
For example:

MyClass.h:
----------

class MyClass
{
map<string, string> m_mapMyMap;
};

MyClass.cpp:
------------

#include "stdafx.h"
#include <string>
#include <map>
using namespace std;
#include "MyClass.h"

------------

Visual Studio .NET 2003 says:
"syntax error: missing ';' before '<' in MyClass.h

Moving the definition of m_mapMyMap into MyClass.cpp compiles fine.

However, it would be nice to be able to use these collection classes
as private data members within an owning class
(as I believe is possible with Java and C#, which do not have similar
template functionality).

Thanks for any info,
Mark
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
* Mark Buxbaum:

Is is possible to use a collection class instance as a helper
object in a class?
Yes.

For example:

MyClass.h:
----------
Here you should #include the headers that [MyClass.h] depends on.
class MyClass
{
map<string, string> m_mapMyMap;
Here you should qualify with "std::" since this is in a header file.
};

MyClass.cpp:
------------

#include "stdafx.h"
#include <string>
#include <map>
using namespace std;
#include "MyClass.h"
As a matter of style that last #include should be the first.

Turn off "precompiled headers" for your project to get standard C++
behavior.

You can turn it on later and adjust things so they work also with
that non-C++ behavior.

"syntax error: missing ';' before '<' in MyClass.h


That has nothing to do with the code shown.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2

P: n/a
ma*********@hotmail.com (Mark Buxbaum) wrote in message news:<fb*************************@posting.google.c om>...
Hi,

Is is possible to use a collection class instance as a helper object in a class?
s/collection/container/
For example:

MyClass.h:
----------

You should

#ifndef INCLUDED_MAP
#include <map>
#define INCLUDED_MAP
#endif

#ifndef INCLUDED_STRING
#include <string>
#define INCLUDED_STRING
#endif

here
class MyClass
{
map<string, string> m_mapMyMap;
This should be

std::map<std::string, std::string> m_mapMyMap;

You *do* *not* want to have a 'using namespace std' statement in your
header file. This is extremely nasty for clients of your 'MyClass'
component who might define types or variables such as 'string' and
'map' (although this is bad practice on their part).

Also, the name 'm_mapMyMap' leaves something to be desired. The 'map'
prefix is redundant and binds your implementation to std::map. If you
change the implementation, you shouldn't have to go through the
trouble of changing the variable name too. Typically, you would pick a
more descriptive name such as 'm_registry' or 'm_dictionary', and give
a comment explaining the data member's use. (I realize you might have
been intentionally generic in your post, but I mention this anyway for
everyone's benefit.)
};

MyClass.cpp:
------------
You *must*

#include "MyClass.h"

as the first included file. This ensures that 'MyClass' is an entirely
self-contained component. That is, clients don't need "special
knowledge" to use it (such as they must also include string and map,
which are really implementation details).
#include "stdafx.h"
#include <string>
#include <map>
You don't need to include string and map again, although it doesn't
hurt (except slightly in compile time since you must find and open
each of these files).
using namespace std;
#include "MyClass.h"


IMO, 'using namespace std' is bad practice, even in an implementation
file. YMMV.

/david
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.