Standard Template Library maps with CString Keys
Richard Bothne -- rbothne@qntm.com
Monday, February 12, 1996
Hi,
I'm trying to use STL with MFC. For the most part everything works fine. I
cannot however get a STL map to work using CString keys. The code is as follows:
#define NOMINMAX
#include
#include
namespace std {
#include "map.h"
}
int main()
{
std::map > m;
return 0;
}
and the error I get is:
D:\C\Delete\function.h(102) : error C2678: binary '<' : no operator defined
which takes a left-hand operand of type 'const class CString' (or there is
no acceptable conversion) (new behavior; please see help)
Note:
I've modified the STL header files as specified in the Docs with VC4.0
Thanks for you help,
Richard
____________________________________________________________________
I used to have a really neat signature file, but I lost it somewhere
Mark Kramer -- mkram@msn.com
Wednesday, February 14, 1996
[Mini-digest: 4 responses]
Richard -
A quick look at afx.h and the CString class along with its accompanying helper
functions should give you a clue. I don't see the operator<() defined that the
compiler is looking for either. Maybe you could define one yourself.
Mark
-----From: "David W. Bickford"
Hi,
In response to "Standard Template Library maps with CString Keys"
I've been using the STL with MFC for quite some time. I too had this =
problem about 3 months ago. I don't know the exact reason why but your =
code works fine on my machine. I can only offer two possible =
explanations:
1) You need to upgrade your copy of the STL from =
ftp.cs.rpi.edu/pub/stl. The version that comes with Visual C++ 4.0 was =
upgraded on October 31st.=20
2) Try dropping the namespace and the code changes, despite the readme. =
I haven't encountered any of those so called incompatibilities. Your =
code works fine.
If anyone encounters further problems or has comments with regard to the =
STL please drop me a note at bickford@ic.net, especially if you can show =
me a code sample that has an incompatibility with MFC.
Dave
-----From: Tim Hagemann <100063.323@compuserve.com>
Hi Richard,
>> I'm trying to use STL with MFC. For the most part everything works fine. I
>> cannot however get a STL map to work using CString keys. The code is as
follows:
I eleminated the error by putting the following declaration in the stdafx.h
header file:
namespace std
{
bool operator<(const CString &s1, const CString &s2)
{
return s1 < s2;
}
}
Hope this helps !
Tim Hagemann
PS: I got several link errors, see my other message.
-----From: Neil Cook
> D:\C\Delete\function.h(102) : error C2678: binary '<' : no operator defined
> which takes a left-hand operand of type 'const class CString' (or there is
> no acceptable conversion) (new behavior; please see help)
>
Richard, the message says that you need to define operator '<' for the
const class CString. If you define one, the message should go away.
When using types which are not part of the language with templates, you will
often have to define such operators.
/Neil
bop@gandalf.se
Wednesday, February 14, 1996
>From Richard Bothne
> Hi,
>
> I'm trying to use STL with MFC. For the most part everything works fine. I
> cannot however get a STL map to work using CString keys. The code is as
> follows:
>
> #define NOMINMAX
> #include
> #include
>
> namespace std {
> #include "map.h"
> }
>
> [snip]
The documentation mentions that CString will conflict with
the STL libarary. The work around is not correct though...
Some of the .h-files in the STL library actually includes standard
C files, like "stdlib.h". The way you coded (i.e. according to
Microsofts hint!) these includes will ALSO be placed in the
namespace "std".
The way to do it, is to edit ALL the STL files (sigh), and place a line
namespace std {
AFTER their includes, and a matching brace at the end of the
file.
Bo Persson
bop@gandalf.se
Peter J Brock -- Peter.Brock@btal.com.au
Monday, February 19, 1996
[Mini-digest: 3 responses]
At 22:46 14/02/96 +0100, you wrote:
>>From Richard Bothne
>> Hi,
>>
>> I'm trying to use STL with MFC. For the most part everything works fine. I
>> cannot however get a STL map to work using CString keys. The code is as
>> follows:
>>
>> #define NOMINMAX
>> #include
>> #include
>>
>> namespace std {
>> #include "map.h"
>> }
>>
>> [snip]
>
>The documentation mentions that CString will conflict with
>the STL libarary. The work around is not correct though...
>
>Some of the .h-files in the STL library actually includes standard
>C files, like "stdlib.h". The way you coded (i.e. according to
>Microsofts hint!) these includes will ALSO be placed in the
>namespace "std".
>
>The way to do it, is to edit ALL the STL files (sigh), and place a line
>
>namespace std {
>
>AFTER their includes, and a matching brace at the end of the
>file.
>
>
>Bo Persson
>bop@gandalf.se
>
>
I have not tried this but, Terris Linenbach has had success.
Check out his home page.
Home page of Terris Linenbach who has modified the STL provided
with the Microsoft Visual C++ 4.0. His modifications are available
from ftp://ftp.rahul.net/pub/terris/stl.zip The code is unique
in that it allows STL to work with CString. Microsoft have a
paper on MFC and Standard Template Library (STL)
BTW My home page which is still evolving (and will continue
to evolve for the next couple of months) has useful links
regarding the Standard Template Library.
http://www.magna.com.au/~peter
Cheers
____________________________________________________________________
Peter J Brock
Senior Analyst/Programmer voice: +61 2 259 3365
Bankers Ttrust Australia Limited fax : +61 2 259 9486
email: Peter.Brock@btal.com.au
Level 2, Chifley Tower, Chifley Square, Sydney NSW 2000, AUSTRALIA
____________________________________________________________________
-----From: Mario Contestabile
>The documentation mentions that CString will conflict with
>the STL libarary. The work around is not correct though...
MSVCs documentation? I haven't found any mention of CString problems
with STL and MFC. I have used CStrings in an STL map.
>Some of the .h-files in the STL library actually includes standard
>C files, like "stdlib.h". The way you coded (i.e. according to
>Microsofts hint!) these includes will ALSO be placed in the
>namespace "std".
>The way to do it, is to edit ALL the STL files (sigh), and place a line
>
>namespace std {
>
>AFTER their includes, and a matching brace at the end of the
>file.
The appropriate method for using STL with MFC is to simply #include "stl.h"
in your source. The stl.h file must #include EVERY standard header which the
stl files #include.
For example.
#define NOMINMAX
#include
#include
#include
namespace std{
#include
}
Modification of every stl header file is not necessary.
mcontest@universal.com
-----From: "David W. Bickford"
I too arrived at this decision. However, I now believe the readme is =
worse than that. I've been combining the STL with CStrings for months =
without a namespace. When I walk through the code in the debugger all =
the appropriate CString global function operators get called. After all =
they take precedence over global templates... Try dispensing with the =
namespace, if you have any problems I'd love to hear about them.
Dave Bickford (bickford@ic.net)
| Вернуться в корень Архива
|