在g ++下使用与std :: vectors的移动语义

时间:2012-01-20 16:59:45

标签: c++ g++ c++11 fedora

我正在尝试在我的VS2010主页上写一个程序来编译大学版的Fedora(Linux version 3.1.9-1.fc16.x86_64 (mockbuild@x86-15.phx2.fedoraproject.org) (gcc version 4.6.2 20111027 (Red Hat 4.6.2-1) (GCC) ))。
该程序使用各种c ++ 11功能,如自动类型和移动构造函数 这在VS2010中都没有问题,但是当我在linux(g++ -g -Wall -std=c++0x -pedantic)中编译它时,我得到了以下一些错误:

In file included from /usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/vector:63:0,
                 from src/OpenGL/Renderer.h:4,
                 from src/Wolfenstein3D2011.cpp:2:
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/stl_construct.h: In function ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = Wulf::MapWall, _Args = {const Wulf::MapWall&}]’:
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/stl_uninitialized.h:77:3:   instantiated from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const Wulf::MapWall*, std::vector<Wulf::MapWall> >, _ForwardIterator = Wulf::MapWall*, bool _TrivialValueTypes = false]’
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/stl_uninitialized.h:119:41:   instantiated from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const Wulf::MapWall*, std::vector<Wulf::MapWall> >, _ForwardIterator = Wulf::MapWall*]’
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/stl_uninitialized.h:259:63:   instantiated from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const Wulf::MapWall*, std::vector<Wulf::MapWall> >, _ForwardIterator = Wulf::MapWall*, _Tp = Wulf::MapWall]’
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/stl_vector.h:280:9:   instantiated from ‘std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = Wulf::MapWall, _Alloc = std::allocator<Wulf::MapWall>, std::vector<_Tp, _Alloc> = std::vector<Wulf::MapWall>]’
src/Map/Map.h:17:8:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/stl_construct.h:76:7: error: no matching function for call to ‘Wulf::MapWall::MapWall(const Wulf::MapWall&)’
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/stl_construct.h:76:7: note: candidates are:
src/Map/MapWall.h:18:3: note: Wulf::MapWall::MapWall(Wulf::MapWall&&)
src/Map/MapWall.h:18:3: note:   no known conversion for argument 1 from ‘const Wulf::MapWall’ to ‘Wulf::MapWall&&’
src/Map/MapWall.h:12:3: note: Wulf::MapWall::MapWall(Wulf::Direction)
src/Map/MapWall.h:12:3: note:   no known conversion for argument 1 from ‘const Wulf::MapWall’ to ‘Wulf::Direction’
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/stl_construct.h: In function ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = Wulf::MapNode, _Args = {const Wulf::MapNode&}]’:
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/stl_uninitialized.h:77:3:   instantiated from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const Wulf::MapNode*, std::vector<Wulf::MapNode> >, _ForwardIterator = Wulf::MapNode*, bool _TrivialValueTypes = false]’
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/stl_uninitialized.h:119:41:   instantiated from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const Wulf::MapNode*, std::vector<Wulf::MapNode> >, _ForwardIterator = Wulf::MapNode*]’
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/stl_uninitialized.h:259:63:   instantiated from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const Wulf::MapNode*, std::vector<Wulf::MapNode> >, _ForwardIterator = Wulf::MapNode*, _Tp = Wulf::MapNode]’
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/stl_vector.h:280:9:   instantiated from ‘std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = Wulf::MapNode, _Alloc = std::allocator<Wulf::MapNode>, std::vector<_Tp, _Alloc> = std::vector<Wulf::MapNode>]’
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/stl_construct.h:76:7:   instantiated from ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = std::vector<Wulf::MapNode>, _Args = {const std::vector<Wulf::MapNode>&}]’
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/stl_uninitialized.h:77:3:   instantiated from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::vector<Wulf::MapNode>*, std::vector<std::vector<Wulf::MapNode> > >, _ForwardIterator = std::vector<Wulf::MapNode>*, bool _TrivialValueTypes = false]’
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/stl_uninitialized.h:119:41:   instantiated from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::vector<Wulf::MapNode>*, std::vector<std::vector<Wulf::MapNode> > >, _ForwardIterator = std::vector<Wulf::MapNode>*]’
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/stl_uninitialized.h:259:63:   instantiated from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::vector<Wulf::MapNode>*, std::vector<std::vector<Wulf::MapNode> > >, _ForwardIterator = std::vector<Wulf::MapNode>*, _Tp = std::vector<Wulf::MapNode>]’
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/stl_vector.h:280:9:   instantiated from ‘std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::vector<Wulf::MapNode>, _Alloc = std::allocator<std::vector<Wulf::MapNode> >, std::vector<_Tp, _Alloc> = std::vector<std::vector<Wulf::MapNode> >]’
src/Map/Map.h:17:8:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/stl_construct.h:76:7: error: no matching function for call to ‘Wulf::MapNode::MapNode(const Wulf::MapNode&)’
/usr/lib/gcc/x86_64-redhat-linux/4.6.2/../../../../include/c++/4.6.2/bits/stl_construct.h:76:7: note: candidates are:
src/Map/MapNode.h:15:3: note: Wulf::MapNode::MapNode(Wulf::MapNode&&)
src/Map/MapNode.h:15:3: note:   no known conversion for argument 1 from ‘const Wulf::MapNode’ to ‘Wulf::MapNode&&’
src/Map/MapNode.h:10:3: note: Wulf::MapNode::MapNode(Wulf::coord, Wulf::coord, Wulf::word, Wulf::word)
src/Map/MapNode.h:10:3: note:   candidate expects 4 arguments, 1 provided
make: *** [obj/main.o] Error 1

由于Map / Map.h:17是class Map {,我不知道是什么造成了这种情况。
我能想象的唯一一条线就是Map / Map.cpp并且是 nodes[x].push_back(MapNode(x - halfwidth, y - halfheight, map[i], objs[i]));

walls.push_back(MapWall(air ? DIRECTION_WEST : DIRECTION_EAST));
这两个在VS2010下工作正常,成功地将临时移动到向量中,但显然不是在g ++下 这是编译器的一些怪癖,还是我做错了什么?

2 个答案:

答案 0 :(得分:3)

据我从错误消息中可以看出,您正在尝试复制包含Map的{​​{1}},但由于vector<MapWall>无法复制,因此失败了。< / p>

如果这些类型应该是可复制的,那么您需要将复制构造函数添加到MapWall;用户声明的移动构造函数意味着不会隐式生成一个。 (我可能会猜测VS2010错误地生成了一个;这可以解释为什么你的代码在那里工作)。

否则,您可能会发现通过向MapWall添加一个私有的,已删除的复制构造函数来更轻松地跟踪恶意复制操作 - 然后您应该收到一条错误消息,告诉您确切要复制它的内容。

答案 1 :(得分:1)

看到这些消息,我确信这是你的错,而且VS2010过于苛刻

这:

src/Map/MapNode.h:15:3: note: Wulf::MapNode::MapNode(Wulf::MapNode&&)
src/Map/MapNode.h:15:3: note:   no known conversion for argument 1 from ‘const Wulf::MapNode’ to ‘Wulf::MapNode&&’
src/Map/MapNode.h:10:3: note: Wulf::MapNode::MapNode(Wulf::coord, Wulf::coord, Wulf::word, Wulf::word)

表示您尝试将lreference对象传递给移动构造函数或实际构造函数。

接下来会出现同样的错误:

no known conversion for argument 1 from ‘const Wulf::MapWall’ to ‘Wulf::MapWall&&’

如果没有看到某些代码,就无法确定你到底做了什么。