我在一个外部库中构建了一个类,我希望在其他领域使用它。但是,当我尝试在 std::vector 中使用该类时,出现编译错误
from ../../src/geom/geom.cpp:21:
/usr/include/c++/4.8.2/ext/new_allocator.h: In instantiation of `void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = GenUtils::MeshNormalData; _Args = {const GenUtils::MeshNormalData&}; _Tp = GenUtils::MeshNormalData]`:
/usr/include/c++/4.8.2/bits/alloc_traits.h:254:4: required from `static typename std::enable_if<std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::value, void>::type std::allocator_traits<_Alloc>::_S_construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = GenUtils::MeshNormalData; _Args = {const GenUtils::MeshNormalData&}; _Alloc = std::allocator<GenUtils::MeshNormalData>; typename std::enable_if<std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::value, void>::type = void]`
/usr/include/c++/4.8.2/bits/alloc_traits.h:393:57: required from `static decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) std::allocator_traits<_Alloc>::construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = GenUtils::MeshNormalData; _Args = {const GenUtils::MeshNormalData&}; _Alloc = std::allocator<GenUtils::MeshNormalData>; decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) = <type error>]`
/usr/include/c++/4.8.2/bits/stl_vector.h:906:34: required from `void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = GenUtils::MeshNormalData; _Alloc = std::allocator<GenUtils::MeshNormalData>; std::vector<_Tp, _Alloc>::value_type = GenUtils::MeshNormalData]’
../../src/geom/geom.cpp:315:47: required from here
/usr/include/c++/4.8.2/ext/new_allocator.h:120:4: error: use of deleted function `GenUtils::MeshNormalData::MeshNormalData(const GenUtils::MeshNormalData&)`
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^
In file included from ../../src/geom/geom.h:60:0,
from ../../src/geom/geom.cpp:21:
../../src/libs/utils/generalUtils.h:94:7: note: `GenUtils::MeshNormalData::MeshNormalData(const GenUtils::MeshNormalData&)` is implicitly deleted because the default definition would be ill-formed:
class MeshNormalData
在库/generalUtils.h
namespace GenUtils
{
class MeshNormalData
{
public:
MeshNormalData(){}
};
}
在 geom.h
#include generalUtils.h
class TestGeo : public Base
{
public:
TestGeo(){};
private:
std::vector< GenUtils::MeshNormalData > meshnormals;
void compute_geo();
};
在geom.cpp中
void TestGeo::compute_geo()
{
meshnormals.clear()
GenUtils::MeshNormalData normals()
//- doing other computation on normals
meshnormals.push_back( normals );
}
我错过了什么?有什么建议吗?需要帮助破译我的错误。
错误的关键部分是它说MeshNormalData
的复制构造函数的定义被删除。
将产生错误是因为程序的某些内容要求复制构造函数不被删除:可访问和可用。
这里需要副本:
meshnormals.push_back( normals );
因为你不写std::move
。
如果它是可移动的,您可以在向量中使用不可复制的类。但是您可能希望使您的类可复制。大概在你的真实代码中,有一些关于防止这种情况的类型。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句