包含<map>后编译错误

Compilation error after including <map>

本文关键字:编译 错误 map lt 包含 gt      更新时间:2023-10-16

我刚开始学习C++,试图学习一些基础知识。现在在Eclipse中使用gcc 5.2.1版本进行编译,项目是使用c++11参数进行编译的。

现在,当我尝试#include <map>时,就会出现include问题(只要编译器进入其中,我甚至还不使用map,只是尝试将其包含在内,这并不重要)。据我所知,地图本身有问题,但似乎不对。此处为简短的故障片段。

08:54:59 **** Incremental Build of configuration Debug for project RpnCalculator ****
make all 
Building file: ../Test/AddShouldBeRegistered.cpp
Invoking: Cross G++ Compiler
g++ -I/home/yoger/Coding/cpputest/include -O0 -g3 -Wall -c -fmessage-    length=0 -std=c++11 -v -MMD -MP -MF"Test/AddShouldBeRegistered.d" -MT"Test/AddShouldBeRegistered.d" -o "Test/AddShouldBeRegistered.o" "../Test/AddShouldBeRegistered.cpp"
Using built-in specs.
COLLECT_GCC=g++
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.2.1-22ubuntu2' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 5.2.1 20151010 (Ubuntu 5.2.1-22ubuntu2) 
COLLECT_GCC_OPTIONS='-I' '/home/yoger/Coding/cpputest/include' '-O0' '-g3' '-Wall' '-c' '-fmessage-length=0' '-std=c++11' '-v' '-MMD' '-MP' '-MF' 'Test/AddShouldBeRegistered.d' '-MT' 'Test/AddShouldBeRegistered.d' '-o' 'Test/AddShouldBeRegistered.o' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-linux-gnu/5/cc1plus -quiet -v -I /home/yoger/Coding/cpputest/include -imultiarch x86_64-linux-gnu -MMD Test/AddShouldBeRegistered.d -MF Test/AddShouldBeRegistered.d -MP -MT Test/AddShouldBeRegistered.d -dD -D_GNU_SOURCE ../Test/AddShouldBeRegistered.cpp -quiet -dumpbase AddShouldBeRegistered.cpp -mtune=generic -march=x86-64 -auxbase-strip Test/AddShouldBeRegistered.o -g3 -O0 -Wall -std=c++11 -version -fmessage-length=0 -fstack-protector-strong -Wformat-security -o /tmp/cc8xNfBA.s
GNU C++11 (Ubuntu 5.2.1-22ubuntu2) version 5.2.1 20151010 (x86_64-linux-gnu)
compiled by GNU C version 5.2.1 20151010, GMP version 6.0.0, MPFR version 3.1.3, MPC version 1.0.3
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring duplicate directory "/usr/include/x86_64-linux-gnu/c++/5"
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/5/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /home/yoger/Coding/cpputest/include
 /usr/include/c++/5
 /usr/include/x86_64-linux-gnu/c++/5
 /usr/include/c++/5/backward
 /usr/lib/gcc/x86_64-linux-gnu/5/include
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/5/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
GNU C++11 (Ubuntu 5.2.1-22ubuntu2) version 5.2.1 20151010 (x86_64-linux-gnu)
compiled by GNU C version 5.2.1 20151010, GMP version 6.0.0, MPFR version 3.1.3, MPC version 1.0.3
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 5ed623d6fe11f4bcc1afee70f77e33b0
In file included from /usr/include/c++/5/map:60:0,
             from ../Test/../MathOperations/MathOperationFactory.h:11,
             from ../Test/ShouldBeRegistered.h:13,
             from ../Test/AddShouldBeRegistered.cpp:9:
/usr/include/c++/5/bits/stl_tree.h: In member function ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_construct_node(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type, _Args&& ...)’:
/usr/include/c++/5/bits/stl_tree.h:528:14: error: ‘__node’ does not name a type
    ::new(__node) _Rb_tree_node<_Val>;
          ^
Test/subdir.mk:66: recipe for target 'Test/AddShouldBeRegistered.o' failed
make: *** [Test/AddShouldBeRegistered.o] Error 1
08:54:59 Build Finished (took 361ms)

好的,我包含地图的文件

#ifndef MATHOPERATIONFACTORY_H_
#define MATHOPERATIONFACTORY_H_
#include <map>
#include <string>
class MathOperation;
class MathOperationFactory {
public:
    MathOperationFactory();
    virtual ~MathOperationFactory();
    virtual MathOperation& findOperationNamed(const std::string &name);
private:
    typedef std::map<std::string, MathOperation*> OperationMap;
    typedef OperationMap::iterator iterator;
    OperationMap _operationsByName;
};
#endif

这是一个与Cpputest的内存泄漏检测功能有关的问题。您可以应用Cpputest文档页面中描述的解决方案之一。

http://cpputest.github.io/manual.html#memory_leak_detection

我可以通过确保<地图>包含在任何CppUTest相关的包含之前。

示例:

class1.h:

#include <map>
.
.
//your code for class 1 business logic

class1_test.cpp:

#include "class1.h"
//other include
#include <CppUTest/TestHarness.h> // at very end

在任何要包含CppUtest标头的地方遵循此规则

所以我没有找到确切的答案,但通过简单地将所有项目文件移到新项目中,成功地完全解决了这个问题。这意味着这个问题在某种程度上与项目设置有关,我比较了两个项目的编译器设置,它们是相同的,所以原因仍然笼罩着一层非常精细的钻石硬雾。