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

Declaring a map in the header file?

P: n/a
I have the following code snippit that the compiler just won't take (vc
++ 6). I want to make a map as a member variable inside a class. So
I've put it into the header file, and it won't take it with the
descriptors, but it doesn't like it if I take them out, either.

(inside my class .h file)
#include <map>

class PRGEdit{
....
protected:
map <CString, Keyword, strCmpkeywordMap();
.....
};
struct strCmp {
bool operator()( const char* s1, const char* s2 ) const {
return strcmp( s1, s2 ) < 0;
}
};

class Keyword{

public:
Keyword();
Keyword(CString newname);
~Keyword();

CString name;
CHARFORMAT* cf;
};

Output:
c:\...\progeditordlg.h(81) : error C2143: syntax error : missing ';'
before '<'
c:\...\progeditordlg.h(81) : error C2501: 'map' : missing storage-
class or type specifiers
c:\...\progeditordlg.h(81) : error C2059: syntax error : '<'
c:\...\progeditordlg.h(81) : error C2238: unexpected token(s)
preceding ';'

I understand that I'm declaring it like it might be used in a .cpp
file, but how do I declare this as a instance variable of the class?

Thanks,
~Scoots

Jun 19 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Scoots wrote:
I have the following code snippit that the compiler just won't take
(vc ++ 6).
Consider getting a better compiler regardless of whether you get this
case to work.
I want to make a map as a member variable inside a class.
So I've put it into the header file, and it won't take it with the
descriptors, but it doesn't like it if I take them out, either.

(inside my class .h file)
#include <map>

class PRGEdit{
...
protected:
map <CString, Keyword, strCmpkeywordMap();
Should be

std::map < ...

(the "std::" part is important). Read up about namespaces.
....
};
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 19 '07 #2

P: n/a
Oh, and yes, the '()' on the keywordMap was something I added in just
to try and get a more descriptive error, the line was:
map <CString, Keyword, strCmpkeywordMap;
Jun 19 '07 #3

P: n/a
ahh, thanks. Forgot the using namespace std; Thanks for the timely
reply.

As for the better compiler, I agree. I've used VS.NET PRO 2003 on my
home machine, but unfotunately, I'm a summer intern here. I don't
have that kind of leverage.

Anyway thanks for the help! I feel slightly rediculous now...

Jun 19 '07 #4

P: n/a
Scoots wrote:
ahh, thanks. Forgot the using namespace std;
Do not put *that* in _the header_!!! I believe there is a FAQ on that

It is also better to use a typedef inside the class to avoid having to
type in all those arguments all the time.
Thanks for the timely
reply.

As for the better compiler, I agree. I've used VS.NET PRO 2003 on my
home machine, but unfotunately, I'm a summer intern here. I don't
have that kind of leverage.
You don't need to leverage it. Just suggest them and make them see
the advantages.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 19 '07 #5

P: n/a
Wow, they don't teach us much right in school do they? I started with
C# (and know it quite well), so there were no header files, and we
could plaster the standard namespace all around in that. Then I went
to college and they taught us java, go figure.

What FAQ would that be? MSDN?

Thanks for your help, but while I was hunting for that reference in
the FAQ, I bumped into the CMapStringToPtr, and since I'm just taking
a string key mapped to a CHARFORMAT*... I don't need any more
complex. This office is using Unicode, so the CString use is going to
save me a lot of conversion (and it's really annoying).

Still, at school I don't code in MFC, so thank you for help, I was
rather mystified.
~Scoots

Jun 19 '07 #6

P: n/a
Scoots wrote:
Wow, they don't teach us much right in school do they? I started with
C# (and know it quite well), so there were no header files, and we
could plaster the standard namespace all around in that. Then I went
to college and they taught us java, go figure.
You're not *prohibited* from putting 'using namespace std;' in a header,
you're strongly discouraged from it. Sorry if I made it sound like it's
disallowed.
What FAQ would that be? MSDN?
http://www.parashift.com/c++-faq-lite/ is commonly accepted as the FAQ
list for this newsgroup.
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 19 '07 #7

P: n/a
Scoots wrote:
Wow, they don't teach us much right in school do they? I started with
C# (and know it quite well), so there were no header files, and we
could plaster the standard namespace all around in that. Then I went
to college and they taught us java, go figure.
Please, keep a little bit of context to ease the reading :)

anyway, you don't put using namespace std in a header because it would
be incpluded in all the source files that include that header.
What FAQ would that be? MSDN?
no, http://www.parashift.com/c++-faq-lite/
Thanks for your help, but while I was hunting for that reference in
the FAQ, I bumped into the CMapStringToPtr, and since I'm just taking
a string key mapped to a CHARFORMAT*... I don't need any more
this looks as a MFC class, that is not part of the c++ standard.
complex. This office is using Unicode, so the CString use is going to
save me a lot of conversion (and it's really annoying).
if you need wide char, you can also consider std::wstring.
>
Still, at school I don't code in MFC, so thank you for help, I was
rather mystified.
Well, the code you posted wasn't about MFC. Fortunately, I would say,
because MFC is OT here ;)

Regards,

Zeppe
Jun 19 '07 #8

P: n/a
On Jun 19, 8:08 am, Scoots <bssalm...@traxcorp.comwrote:
Wow, they don't teach us much right in school do they? I started with
C# (and know it quite well), so there were no header files, and we
could plaster the standard namespace all around in that. Then I went
to college and they taught us java, go figure.

What FAQ would that be? MSDN?

Thanks for your help, but while I was hunting for that reference in
the FAQ, I bumped into the CMapStringToPtr, and since I'm just taking
a string key mapped to a CHARFORMAT*... I don't need any more
complex. This office is using Unicode, so the CString use is going to
save me a lot of conversion (and it's really annoying).

Still, at school I don't code in MFC, so thank you for help, I was
rather mystified.
~Scoots
http://www.parashift.com/c++-faq-lit....html#faq-27.5

Jun 19 '07 #9

P: n/a
On Tue, 19 Jun 2007 06:51:20 -0700 in comp.lang.c++, Scoots
<bs*******@traxcorp.comwrote,
>++ 6). I want to make a map as a member variable inside a class. So
....
>class PRGEdit{
...
protected:
map <CString, Keyword, strCmpkeywordMap();
You do realize that is a function declaration, not a member variable?
Jun 19 '07 #10

P: n/a
Sorry about the mfc reference there. I posted the question here, as
originally, I was using an STL map and I figured this was better than
the MFC group for that. It was only later I discovered the MFC
solution.

Zeppe: I haven't tried wstring, but I can't get the compiler to
recognize a 'string' either. I didn't try to hard, so it might be
another namespace conflict, but as the rest of the code I was editing
was written with LPSTR and CStrings I've continued using them.

Thanks all of you for the FAQ, there's some good answers in there!

And finally, David Harmon: Yeah, I know. I actually replied to my
own message about that. I'd tried to get the compiler to be a bit
more explicit about it's error (and it didn't help), the line should
have read 'map <CString, Keyword, strCmpkeywordMap;' But Victor had
already replied before my clarification, so I deleted it. You'll also
notice that my comparison function wouldn't work with CStrings either.

Thanks guys, problem solved.

Jun 19 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.