15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


VC++ 4.1 and STL

Serge Lalonde -- serge@infolytica.qc.ca
Thursday, September 19, 1996

Environment: Win NT 3.51, VC++ 4.1

Hi there!

Has anyone successfully used MFC and the STL supplied with VC++ 4.1?
I've almost got it working (after defining NOMINMAX), but I still
have teh following 2 compilation errors in some files (sometimes
repeated several times). The error is in defalloc.h

\stl\include\defalloc.h(51) : error C2665: 'new' : none of the 4 overloads can convert parameter 1 from type 'char [26]' (new behavior; please see help)

\stl\include\defalloc.h(124) : error C2061: syntax error : identifier 'p'

The offending lines in defalloc.h are

template 
inline T* allocate(int size, T*) {
    set_new_handler(0);
    T* tmp = (T*)(::operator new((unsigned int)(size * sizeof(T)))); <- Line 51
    if (tmp == 0) {
        cerr << "out of memory" << endl;
        exit(1);
    }
    return tmp;
}

template 
inline void construct(T1* p, const T2& value) {
    new (p) T1(value); <- Line 124
}

I've also tried using the STL library supplied with Mark Nelson's book "C++
Programmers Guide to the STL" (an excellent book, by the way), but got the
same results.

I'll be upgrading to VC++ 4.2 soon, but not before I can get this working
for a demo (catch 22?).

Any help would be greatly appreciated. Thanks.
--
Have a tremendous day!
    _/_/_/_/ _/_/_/_/ _/_/_/    _/_/_/  _/_/_/_//  300 Leo Parizeau, Suite 2222
   _/       _/       _/    _/ _/       _/      /  Montreal, PQ, Canada H2W 2P4
  _/_/_/_/ _/_/_/   _/_/_/_/ _/  _/_/ _/_/_/  /  Voice: (514) 849-8752 x236
       _/ _/       _/  _/   _/    _/ _/      /__Fax: (514) 849-4239__
_/_/_/_/ _/_/_/_/ _/    _/   _/_/_/ _/_/_/_/ Lalonde, Infolytica Corp.



Chris Downs -- chris.downs@slug.org
Sunday, September 22, 1996

[Mini-digest: 6 responses]

 > Has anyone successfully used MFC and the STL supplied with VC++ 4.1?
 > I've almost got it working (after defining NOMINMAX), but I still
 > have teh following 2 compilation errors in some files (sometimes
 > repeated several times). The error is in defalloc.h

 > \stl\include\defalloc.h(51) : error C2665: 'new' : none of the 4
 > overloads can  convert parameter 1 from type 'char [26]' (new behavior;

 You need to #include  _outside_ of the std namespace.  So
 you need to do something like:

 #include 
 #include 
 namespace std
 {
   #include 
   //other STL includes
 }

 > I've also tried using the STL library supplied with Mark Nelson's book
 > "C++ Programmers Guide to the STL" (an excellent book, by the way), but
 > got the same results.

 Yep.  Nelson's book was written before VC could handle the STL.

 > I'll be upgrading to VC++ 4.2 soon, but not before I can get this
 > working for a demo (catch 22?).

 You'll be in for quite a surprise.  Once you get it working in 4.1,
 it won't work anymore in 4.2.


-----From: John Addis 

I believe that including  should help.

-- 
John Addis        Master of Time and Space
jaddis@erols.com  C++, MFC, Win32, Win95, TCP/IP
"Invalid or missing REALITY.COM Universe halted."
-----From: Greg_Young@cda.com

     From FAQ 11.27:
     
      The trick is to include "new.h" (and also "iostream.h"  for similar 
     reasons) before you include any stl headers.  For example:
        
        #include 
     
        #include 
        
        namespace std {
     
        #include 
     
        }
     
     
     -- Greg Young


-----From: Lars Schouw 

Hi Serge.

The sollution is to descriobes on the MS WWW server or on the developers
network 
CD:

  
PSS ID Number: Q143207
Article last modified on 01-30-1996
 
4.00
 
WINDOWS NT
 

-----------------------------------------------------------------------
The information in this article applies to:
 
 - Microsoft Visual C++, 32-bit Edition, version 4.0
-----------------------------------------------------------------------
 
SYMPTOMS
========
 
Case One
--------
 
When compiling a file that uses the Standard Template Library (STL), you
may see the following warning and error when using the std namespace, which
is required when using the STL with MFC:
 
   {stl directory}\iterator.h(65) : warning C4114: same type qualifier
   used more than once
   {stl directory}\defalloc.h(124) : error C2661: 'new' : no overloaded
   function takes 2 parameters
 
Case Two
--------
 
In addition, you may get this different set of errors:
 
   {include directory}\new.h(80) : error C2061: syntax error :
   identifier 'THIS_FILE'
   {include directory}\new.h(80) : error C2091: function returns
   function
   {include directory}\new.h(80) : error C2809: 'operator new' has no
   formal parameters#endif
   {include directory}\new.h(80) : error C2065: 'ptr' : undeclared
   identifier
 
Case Three
----------
 
Once the previous two cases are resolved, you may get link errors. The
errors you may receive take this form:
 
   error LNK2001: unresolved external symbol "SymbolName"
 
Here SymbolName is a C Run-Time (CRT) symbol prefaced by the std::
namespace name.
 
RESOLUTION
==========
 
Warning C4114 is superfluous and can be ignored. You can disable the
warning by using a '#pragma warning' directive. Error C2661 requires that
you include New.h in your source file.
 
The second set of errors occurs if you add the STL include directive after
the following definitions and do not include New.h among your include
directives:
 
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
 
Including New.h will work around this problem as well. Note that these
definitions are no longer necessary in MFC code and can be deleted.
 
The third set of errors from the linker can be resolved by including the
appropriate header files from the CRT above the declaration for the std
namspace. You can determine which header file must be included by looking
at the symbol listed in the linker error. Remove the leading std:: from the
symbol, and then search for the symbol in the CRT header files. This file
was included by the STL headers. You need to include this header explicitly
before the namespace declaration. In the Case Three Sample Code section in
this article, you can see Iostream.h is the header that must be included at
global scope.
 
STATUS
======
 
This behavior is by design.
 
MORE INFORMATION
================
 
Sample Code to Reproduces Behavior Described in Case One
--------------------------------------------------------
 
/* Compile options needed: none
*/
 
//#include    // Required to avoid C2661 error.
 
namespace std {
#include 
}
 
std::vector test;
 
Sample Code to Reproduces Behavior Described in Case Two
--------------------------------------------------------
 
/* Compile options needed: /c /D_DEBUG /DNOMINMAX
*/
 
#include   // or stdafx.h in a default AppWizard project.
//#include    // Required to avoid this problem.
 
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
 
namespace std {
#include 
}
 
Sample Code to Reproduces Behavior Described in Case three
----------------------------------------------------------
 
/* Compile options needed: none
*/
 
//#include      // Required to avoid LNK2001 error
#include 
 
namespace std {
#include 
}
 
std::vector test(0,10);
 
Additional reference words: 4.00 MFC
KBCategory: kbtool kberrmsg kbprb
KBSubcategory: CPPIss
=============================================================================
Copyright Microsoft Corporation 1996.

You will recieve the stl.h implementation by email.



-----From: Mario Contestabile


>Has anyone successfully used MFC and the STL supplied with VC++ 4.1?

STL wasn't supplied with MSVC until 4.2? With 4.1,
you have to use namespaces to differeciate between operator new
in defalloc.h and Microsoft's new.h because the wrong one may end up getting 
called.
If you are using HP's STL, defalloc needs the following lines changed:
defalloc.h(135) return std::allocate((difference_type)n, (pointer)0);
defalloc.h(137) void deallocate(pointer p) { std::deallocate(p); }
Notice I used "std" as the namespace.

>I'll be upgrading to VC++ 4.2 soon, but not before I can get this working

Microsoft does supply STL with 4.2, and you won't need to use namepaces,
although you will have to modify their stl.h file.

mcontest@universal.com

-----From: "C.Zhang" 

Try the following format:

T* tmp = (T*)(::operator new CHAR[size * sizeof(T)]);






| Вернуться в корень Архива |