Using STL with MFC
Richard Brice -- bricer@wsdot.wa.gov Friday, January 10, 1997 Environment: VC++ 4.2-flat, Win 95, NT 4.0 How do I use STL with MFC? I get a lot of compiler errors and the namespace std { #include} std::vector.Method(); technique doesn't seem to be working. If anyone has successfully implemented STL with MFC, I'd like to see an example.
Jim Byrd -- byrd@acm.org Saturday, January 11, 1997 [Mini-digest: 2 responses] At 07:41 AM 1/10/97 PST, Richard Bricewrote: > >Environment: VC++ 4.2-flat, Win 95, NT 4.0 > >How do I use STL with MFC? As others have said several times, don't use VC++ 4.2-flat. Use the patch to get 4.2b. Then try again. -----From: Frank Pijpers At 07:41 1/10/97 PST, Richard Brice wrote: >How do I use STL with MFC? On Microsoft's website for Visual C++ (http://www.microsoft.com/visualc) there is an article dedicated to the combined use of MFC & STL. From that article the following part pertaining to your question has been taken: --- Using STL with the Windows Header Files=20 Problem:=20 The min and max functions conflict when using STL if NOMINMAX is not= defined. The file windef.h defines the macros min and max as follows:=20 #define max(a,b) (((a) > (b)) ? (a) : (b)) #define min(a,b) (((a) < (b)) ? (a) : (b)) STL defines two function templates min and max as follows:=20 inline const T& max(const T&a, const T&b); inline const T& min(const T&a, const T&b); The min and max macros interfere with the STL min and max template functions. Therefore, if you wish to mix STL and Windows headers we suggest you define the macro NOMINMAX , which has the effect of inhibiting the definition of min and max in windef.h.=20 Mixing MFC and STL Requires Use of Namespaces=20 Problem:=20 Several functions in MFC and STL have the same names.=20 MFC defines global equality operators, for example:=20 BOOL AFXAPI operator=3D=3D(const CString& s1, const CString& s2); STL, on the other hand, defines templatized global equality operators, for example:=20 bool operator =3D=3D(const T& x, const T& y); If STL and MFC headers are mixed, it can lead to the wrong operator being called. For example, an operator may be generated from an STL templatized operator when an MFC operator should have been called directly. To avoid this, wrap the STL headers in a namespace, for example:=20 namespace std { #include } The name std is recommend by ANSI for the standard template library namespace. All STL classes and functions should then be accessed, either explicitly or by using-declarations, for example:=20 int f() { std::vector v; } int g() { using std::vector; vector v; } For more information see the Namespaces topic in the Visual C++ Language Reference.=20 Once you have wrapped the STL headers in the std namespace you need to qualify STL global function calls with std. For example, ::swap(start, x.start) would become std::swap(start, x.start). Calls to global operators new and delete should NOT be so qualified. The changes you need to make are in:=20 bvector.h(317): ::swap(start, x.start);=20 bvector.h(318): ::swap(finish, x.finish); bvector.h(319): ::swap(end_of_storage, x.end_of_storage); defalloc.h(138): return ::allocate((difference_type)n, (pointer)0); defalloc.h(140): void deallocate(pointer p) { ::deallocate(p); } deque.h(289): ::swap(start, x.start); deque.h(290): ::swap(finish, x.finish); deque.h(291): ::swap(length, x.length); deque.h(292): ::swap(map, x.map); deque.h(293): ::swap(map_size, x.map_size); list.h(195): ::swap(node, x.node); list.h(196): ::swap(length, x.length); tree.h(381): ::swap(header, t.header); tree.h(382): ::swap(node_count, t.node_count); tree.h(383): ::swap(insert_always, t.insert_always); tree.h(384): ::swap(key_compare, t.key_compare); tree.h(656): ::swap(color(y), color(z)); tree.h(657): ::swap(y, z); vector.h(123): ::swap(start, x.start); vector.h(124): ::swap(finish, x.finish); vector.h(125): ::swap(end_of_storage, x.end_of_storage);=20 =A9 1997 Microsoft Corporation. All rights reserved. Legal Notices.=20 --- Hopefully this answers your question, Frank
| Вернуться в корень Архива |