0

I have built a class in an external library that I was hoping to use in other areas. However, when I try to use the class within an std::vector I get compile errors

                 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

in libs/generalUtils.h

namespace GenUtils
{
   class MeshNormalData
   {
   public:
      MeshNormalData(){}
   };
}

in geom.h

#include generalUtils.h

class TestGeo : public Base
{
public:
    TestGeo(){};
private:
    std::vector< GenUtils::MeshNormalData > meshnormals;
    void compute_geo();
};

in geom.cpp

void TestGeo::compute_geo()
{
    meshnormals.clear()
    GenUtils::MeshNormalData normals()
    //- doing other computation on normals
    meshnormals.push_back( normals );
}

What am I missing? Any advice? Need help deciphering my error.

jozef3d
  • 3
  • 2
  • It looks like you're missing the copy constructor of `MeshNormalData`, which was "implicitly deleted because the default definition would be ill-formed". Show us what `MeshNormalData` is. If `MeshNormalData` is non-copyable, but movable, try `meshnormals.push_back(std::move(normals));`. – Evg Nov 30 '19 at 14:52
  • Thanks added the copy constructor to my MeshNormalData and worked. – jozef3d Nov 30 '19 at 15:03
  • This is not a real testcase. e.g. `normals` is a function. Don't make us guess. Present a real [mcve]. – Lightness Races in Orbit Nov 30 '19 at 15:03

3 Answers3

0

The key part of the error is where it says the definition of MeshNormalData's copy constructor is deleted.

The error will be generated because something about the program requires the copy constructor to not be deleted: to be accessible and usable.

The copy is needed here:

meshnormals.push_back( normals );

because you do not write std::move.

You can get away with a non-copyable class in a vector if it's moveable. But you may wish to make your class copyable. Presumably in your real code there's something about the type that prevents that.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

You need to provide the copy constructor for the class. There is no problem with Vector as you mentioned in question title, its class MeshNormalData. Push_back expect the copy constructor.

Below link can help you more: What are all the member-functions created by compiler for a class? Does that happen all the time?

Build Succeeded
  • 1,153
  • 1
  • 10
  • 24
0

Added a copy constructor to make the it copyable.

namespace GenUtils
{
   class MeshNormalData
   {
   public:
      MeshNormalData(){}
      MeshNormalData( const MeshNormalData &obj){};    // copy 
   };
}

using meshnormals.push_back(std::move(normals)); worked too.

Thanks Evg!

jozef3d
  • 3
  • 2