Проблема в том числе заголовочные файлы STL - PullRequest
3 голосов
/ 05 августа 2011

Я создал приложение на основе диалога в MFC (VS 2008). Когда я делаю "#include" в Dlg.cpp, оно показывает следующую ошибку.

В установке VS 2008 отсутствует какой-либо компонент

c:\program files (x86)\microsoft visual studio 9.0\vc\include\xmemory(43) : 
error C2665: 'operator new' : none of the 5 overloads could convert all the argument types

1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\new.h(85): could be 'void *operator new(size_t,const std::nothrow_t &) throw()'

1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\new.h(93): or       'void *operator new(size_t,void *)'

1>        while trying to match the argument list '(const char [70], int)'

1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\xmemory(145) : see reference to function template instantiation '_Ty *std::_Allocate<char>(size_t,_Ty *)' being compiled

1>        with

1>        [
1>            _Ty=char
1>        ]

1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\xmemory(144) : while compiling class template member function 'char *std::allocator<_Ty>::allocate(std::allocator<_Ty>::size_type)'

1>        with
1>        [
1>            _Ty=char
1>        ]

1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\xstring(2216) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled

1>        with
1>        [
1>            _Ty=char
1>        ]

1>Build log was saved at "file://c:\Users\Public\Documents\Proj\STL1\STL1\Debug\BuildLog.htm"
1>STL1 - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

1 Ответ

7 голосов
/ 05 августа 2011

Любой STL, включенный в ваш проект MFC, должен быть включен до определения DEBUG_NEW. В прошлом это было проблемой (похоже, больше нет, поскольку я не могу воспроизвести ее в VS 2010).

// myfile.cpp
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// This will cause the error
#include <vector>

Принимая во внимание

// myfile.cpp
// will work OK
#include <vector>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

DEBUG_NEW - это расширение для Microsoft, которое помогает обнаруживать утечки памяти в отладочных версиях приложения. Это может быть очень полезно в некоторых случаях.

Он определен в afx.h как:

void* AFX_CDECL operator new(size_t nSize, LPCSTR lpszFileName, int nLine);
#define DEBUG_NEW new(THIS_FILE, __LINE__)`

Это помогает обеспечить отслеживание выделенной памяти, а при обнаружении утечки даст вам файл и номер строки, где произошло выделение.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...