Can't include <algorithm>

I have an MFC Doc/View application. I'm using VC6.0 (SP5)/W2K.

Why after adding the line
#include <algorithm>
I'm getting the following errors?

c:\archivos de programa\microsoft visual studio\vc98\include\new(35) : error C2061: syntax error : identifier 'THIS_FILE'
c:\archivos de programa\microsoft visual studio\vc98\include\new(35) : error C2091: function returns function
c:\archivos de programa\microsoft visual studio\vc98\include\new(35) : error C2809: 'operator new' has no formal parameters
c:\archivos de programa\microsoft visual studio\vc98\include\new(36) : error C2061: syntax error : identifier 'THIS_FILE'
c:\archivos de programa\microsoft visual studio\vc98\include\new(37) : error C2091: function returns function
c:\archivos de programa\microsoft visual studio\vc98\include\new(37) : error C2556: 'void *(__cdecl *__cdecl operator new(void))(unsigned int,const struct std::nothrow_t &)' : overloaded function differs only by return type from 'void *(__cdecl *__c
decl operator new(void))(unsigned int)'
        c:\archivos de programa\microsoft visual studio\vc98\include\new(35) : see declaration of 'new'
c:\archivos de programa\microsoft visual studio\vc98\include\memory(16) : error C2061: syntax error : identifier 'THIS_FILE'
c:\archivos de programa\microsoft visual studio\vc98\include\memory(17) : error C2091: function returns function
c:\archivos de programa\microsoft visual studio\vc98\include\memory(17) : error C2784: 'void *(__cdecl *__cdecl operator new(void))(unsigned int,class std::allocator<`template-parameter257'> &)' : could not deduce template argument for 'void *(__cde
cl *)(unsigned int,class std::allocator<_Ty> &)' from 'void *(__cdecl *)(unsigned int)'
c:\archivos de programa\microsoft visual studio\vc98\include\memory(17) : error C2785: 'void *(__cdecl *__cdecl operator new(void))(unsigned int,class std::allocator<`template-parameter257'> &)' and 'void *(__cdecl *__cdecl operator new(void))(unsig
ned int)' have different return types
        c:\archivos de programa\microsoft visual studio\vc98\include\memory(16) : see declaration of 'new'
c:\archivos de programa\microsoft visual studio\vc98\include\memory(17) : error C2809: 'operator new' has no formal parameters
c:\archivos de programa\microsoft visual studio\vc98\include\memory(20) : error C2954: template definitions cannot nest





 
elitoAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
fl0ydConnect With a Mentor Commented:
Generally warnings are just that - they inform the developer about conditions that might not be as he/she intended. They are to be taken seriously and at least checked to see what's causing them but they are certainly different from errors. Once you've checked what the cause of the warning is and think it's not a problem you can instruct the compiler to not issue this specific warning again with
#pragma warning( disable : 4100 )

Here is a piece of code that will issue warnings to show you what their nature is:

int main( int argc, char* argv[] ) {
    int unused_integer;    // warning that this variable is not used
    for( int i = 0; i < 1; ++i );    // warning of an empty statement
    return 0;
}
0
 
elitoAuthor Commented:
The same problem happens with #include <vector>


0
 
mblatCommented:
Make sure you are including STL headers before

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

block of code in .cpp files....  Make sure you don't include you headers that have

"include <vector> after that block of code...


Hope it helps..
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
nke2000Commented:
elito,
can you give more info?  what else are you including? in what header are you including this, etc?

0
 
elitoAuthor Commented:
Hi,

This is the code I'm using:

#if !defined(AFX_PREFERENCESSHEET_H__346E3613_CE6D_4AAE_827A_A3A72A586726__INCLUDED_)
#define AFX_PREFERENCESSHEET_H__346E3613_CE6D_4AAE_827A_A3A72A586726__INCLUDED_


#include "Preferences.h"     // Added by ClassView


#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// PreferencesSheet.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CPreferencesSheet

#include "Page1.h"
#include "Page2.h"
#include "Page3.h"
#include "Page5.h"
#include "Page6.h"     // Added by ClassView
#include "Page7.h"
#include "Page8.h"



#include <vector>


class CPreferencesSheet : public CPropertySheet
{
     DECLARE_DYNAMIC(CPreferencesSheet)

// Construction
public:
     CPreferencesSheet(UINT nIDCaption, CWnd* pParentWnd = NULL, UINT iSelectPage = 0);
     CPreferencesSheet(LPCTSTR pszCaption, CWnd* pParentWnd = NULL, UINT iSelectPage = 0);

// Attributes
public:    
     CPage1          m_Page1;
     CPage2          m_Page2;
     CPage3          m_Page3;
     CPage4          m_Page4;
     CPage5          m_Page5;
     CPage6          m_Page6;
     CPage7          m_Page7;
     CPage8          m_Page8;


// Operations
public:
     CPreferences m_Preferences;
     

// Overrides
     // ClassWizard generated virtual function overrides
     //{{AFX_VIRTUAL(CPreferencesSheet)
     public:
     virtual BOOL OnInitDialog();
     //}}AFX_VIRTUAL

// Implementation
public:virtual ~CPreferencesSheet();
     CFont m_NewFont;
     CFont m_ButtonFont;
     CPrefPeakPickingPage m_PeakPickingPage;

     // Generated message map functions
protected:
     //{{AFX_MSG(CPreferencesSheet)
          // NOTE - the ClassWizard will add and remove member functions here.
     //}}AFX_MSG
     DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_PREFERENCESSHEET_H__346E3613_CE6D_4AAE_827A_A3A72A586726__INCLUDED_)
0
 
mblatCommented:
2 elito:

did you read my comment?  That really should be your problem.
0
 
elitoAuthor Commented:
mblat,

I'm not including any STL header in a cpp file, only in the .h file, as shown in the code above.
0
 
mblatCommented:
You header file that includes STL header has to be BEFORE

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

also.  Do you do that?
0
 
nke2000Commented:
I don't see the code mblat is talking about in your header.  

Move it up above #include "preferences.h" and see if that helps.
0
 
mblatCommented:
2 nke2000
Code I am talking about is in .cpp file.  What I am saying is that all STL headers have to be included BEFORE that.  
Read all posts in this thread and you see where I am mentioning that.
0
 
fl0ydCommented:
Errr, excuse me... Including header files in a header file is bad, bad, BAD practice. Use forward declarations whenever possible or make use of the precompiled header file through stdafx.h and include all those files that rarely/never change in there. Once your application has several hundred classes, maybe even thousands, you will notice how much faster compilation will be. This also decreases the chances for getting circular dependencies that cause the compiler to stop compiling the project altogether.
0
 
elitoAuthor Commented:
mblat,

I'm sorry but I don't really understand your comment. You are saying that I should include the STL headers before this:

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


But this code only appears in cpp files, NOT in header files. As I said before, I'm including the STL header files in the header file (see my code snippet above).

Can you please see more specific?
elito
0
 
nke2000Commented:
Move #include <vector> up above #include "preferences.h" and see if that helps.

I don't agree with fl0yd's thesis that including headers in headers is a "bad, bad, BAD" practice.  Foolishly including headers in headers is a bad practice.  Show me a Windows or STL header that doesn't include another header and I'll show you an anamoly!
0
 
mblatCommented:
So you header file hould ne include in .cpp BEFORE this blok of code.

in
MyFile.h

.....

#include <algorithm>

.....


in MyFile.cpp

#include "stdafx.h"
#include "MyFile.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

Just a note: you error is typical compilation error when invalid sequence of .h file is used. Microsoft has a TN on the matter called
PRB: Common Problems When Using STL with STD Namespace

it says it applyes to 4.0, but since MS hasn't updated STL library since 4.0 it also applyes to later versions, ( and I run into it with 6.0 also)
0
 
elitoAuthor Commented:
Yes, I'm including my header file before that block of code in the cpp file.
I'm still don't know how to circunvent this problem.
0
 
nke2000Commented:
elito,
did you try my suggestion?

--NKE
0
 
fl0ydCommented:
@ nke:
Show me some source that keeps a compiler busy for an hour and I'll show you how to cut down this time to half by applying a few guidelines like the one I suggested. All of a sudden this "thesis" turns into measurable timespans.

@ elito:
Put all of your STL-includes in your stdafx.h-file. Make sure the STL-includes are included BEFORE any other header file is included. The problem you are facing is that MFC #define's the 'new'-operator with its own version for debug reasons [if you compile your code in Relase-Build I'm sure you're having not problem]. This DEBUG_NEW is not compatible with the standard new-operator - which is what STL is using.
0
 
elitoAuthor Commented:
Well, I have moved the STL- includes to the stdafx.h file and now the program compiles without errors. However, I'm getting a lot of warnings. Most of them are related to xmemory. For example:

:\archivos de programa\microsoft visual studio\vc98\include\xmemory(37) : warning C4100: '_P' : unreferenced formal parameter
        c:\archivos de programa\microsoft visual studio\vc98\include\xmemory(68) : see reference to function template instantiation 'void __cdecl std::_Destroy(unsigned int *)' being compiled

Is this normal? The programs seems to work properly, but I don't know if this could cause any undesired secondary effect.

Thanks for your help.
0
 
elitoAuthor Commented:
Thanks fl0yd for the help!
0
 
nke2000Commented:
fl0yd,
I agree, foolishly adding headers causes problems, but including headers in headers prudently is okay.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.