Declaring a map in the header file?

S

Scoots

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, strCmp> keywordMap();
.....
};


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
 
V

Victor Bazarov

Scoots said:
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, strCmp> keywordMap();

Should be

std::map < ...

(the "std::" part is important). Read up about namespaces.
....
};
[..]

V
 
S

Scoots

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, strCmp> keywordMap;
 
S

Scoots

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...
 
V

Victor Bazarov

Scoots said:
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
 
S

Scoots

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
 
V

Victor Bazarov

Scoots said:
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
 
Z

Zeppe

Scoots said:
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
 
N

Naresh Rautela

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-lite/coding-standards.html#faq-27.5
 
D

David Harmon

On Tue, 19 Jun 2007 06:51:20 -0700 in comp.lang.c++, Scoots
++ 6). I want to make a map as a member variable inside a class. So ....

class PRGEdit{
...
protected:
map <CString, Keyword, strCmp> keywordMap();

You do realize that is a function declaration, not a member variable?
 
S

Scoots

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, strCmp> keywordMap;' 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,174
Latest member
BlissKetoACV
Top