Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Declaring a map in the header file?

Reply
Thread Tools

Declaring a map in the header file?

 
 
Scoots
Guest
Posts: n/a
 
      06-19-2007
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-19-2007
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, strCmp> keywordMap();


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


 
Reply With Quote
 
 
 
 
Scoots
Guest
Posts: n/a
 
      06-19-2007
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;


 
Reply With Quote
 
Scoots
Guest
Posts: n/a
 
      06-19-2007
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...

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      06-19-2007
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


 
Reply With Quote
 
Scoots
Guest
Posts: n/a
 
      06-19-2007
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

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      06-19-2007
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


 
Reply With Quote
 
Zeppe
Guest
Posts: n/a
 
      06-19-2007
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
 
Reply With Quote
 
Naresh Rautela
Guest
Posts: n/a
 
      06-19-2007
On Jun 19, 8:08 am, Scoots <bssalm...@traxcorp.com> 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.
>
> 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

 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      06-19-2007
On Tue, 19 Jun 2007 06:51:20 -0700 in comp.lang.c++, Scoots
<> wrote,
>++ 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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Header files with "header.h" or <header.h> ?? mlt C++ 2 01-31-2009 02:54 PM
Problems declaring a map Enric C++ 10 11-04-2007 06:16 PM
Forward Declaring enums' for header? Travis C++ 1 08-15-2007 04:52 PM
Trouble Declaring 3D Array in Header File free2klim C++ 2 07-21-2006 05:05 PM
Re: std::map ,errors and warnings when declaring John Harrison C++ 0 08-24-2003 08:38 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57