向量类

时间:2018-01-13 08:31:24

标签: c++11 vector

我有一个非常简单的结构A,并希望在矢量中使用它,比如说std :: vector。我应该如何定义所需的复制/移动构造函数和赋值?我试过要么不定义复制/移动构造函数或赋值,要么明确定义它们。但无论哪种方式都行不通。这应该是一件非常简单的事情。我很震惊,我无法得到它!

  • 未定义复制/移动构造函数或赋值

    struct A  
    {
      std::string key;
      std::string key2;
    
      A() {};
      A(std::string key, std::string key2): key(key), key2(key2) {};
    };
    
  • 用户定义复制/移动构造函数和赋值

    struct A
    {
      std::string key;
      std::string key2;
    
      A() {};
    
      A(std::string key, std::string key2): key(key), key2(key2) {};
    
      A(const A& other): key(other.key),key2(other.key2){} ;
      A& operator=(const A& other) {
           key = other.key;
           key2 = other.key2;
           return *this;
      };
    
      A(A&& other): key(std::move(other.key)),key2(std::move(other.key2)){} ;
      A& operator=(A&& other) {
        if (this == &other)
           return *this;
    
        key = std::move(other.key);
        key2 = std::move(other.key2);
        return *this;
      };
    };
    

我还尝试使用默认的移动/复制构造函数和赋值,通过赋值" = default"这四个功能。没有帮助。

使用结构A的地方

    class B {
    private:
      std::vector<A> A_v;
    public:
      // first call preAllocate, to preallocate and initialize 1000 entries.
      void preAllocate(){
        A_v.resize(1000);
      }

      // call addA() with id starting from 0. If fewer than 100 entries are 
      // left, call resize() to expand. 
      // We don't know the total number of entries for A.
      // and addA() is called out of order in turn of id, 
      // so, I couldn't use push_back().
      void addA(int id, A& a){

        assert(id < A_v.size());
        A_v[id].key = a.key;
        A_v[id].key2 = a.key2;

        if (A_v.size() - id < 100)
           A_v.resize(A_v.size() + 100);
      }
    };

当我运行程序时,由于vector :: resize()调用,我看到了分段错误错误。

  

编程接收信号SIGSEGV,分段故障。 [切换到   线程0x7fffe97fa700(LWP 88073)] 0x00007ffff7b91268 in   std :: basic_string,std :: allocator

     
    来自/usr/lib/x86_64-linux-gnu/libstdc++.so.6(gdb)bt的

:: basic_string(std :: string const&amp;)()     #d 0x00007ffff7b91268在std :: basic_string,std :: allocator中     :: basic_string(std :: string const&amp;)()来自/usr/lib/x86_64-linux-gnu/libstdc++.so.6
    A :: A中的#1 0x000000000041e02f(这= 0x7ffff7ef2ee0,         其他= ...)在objrecipe.h:34
    #2 0x0000000000430d88 in std :: _ Construct&gt; (         __p = 0x7ffff7ef2ee0)atus /include/c++/4.9/bits/stl_construct.h:75
    std :: __ uninitialized_copy :: __ uninit_copy中的#3 0x000000000042fb5f     (__ first = 0x7ffff7f8fee0,__last = 0x7ffff7fab210,         __result = 0x7ffff7ec0010)         在/usr/include/c++/4.9/bits/stl_uninitialized.h:75
    #4 0x000000000042db7f在std :: uninitialized_copy中(__ first = 0x7ffff7f5d010,__last = 0x7ffff7fab210,     __result = 0x7ffff7ec0010)         at /usr/include/c++/4.9/bits/stl_uninitialized.h:125
    #5 0x000000000042ae5a in std :: __ uninitialized_copy_a(__ first = 0x7ffff7f5d010,__last = 0x7ffff7fab210,         __result = 0x7ffff7ec0010)
        at /usr/include/c++/4.9/bits/stl_uninitialized.h:278
    #6 0x00000000004272f0 in std :: __ uninitialized_move_if_noexcept_a&gt; (__first = 0x7ffff7f5d010,         __last = 0x7ffff7fab210,__ result = 0x7ffff7ec0010,__ alloc = ...)         在/usr/include/c++/4.9/bits/stl_uninitialized.h:301
    #7 0x0000000000423520 in std :: vector&gt; :: _ M_default_append(this = 0x7ffff268cb78,__n = 1000)         在/usr/include/c++/4.9/bits/vector.tcc:561
    #8 0x000000000041f821在std :: vector&gt; :: resize(this = 0x7ffff268cb78,__ new_size = 21000)         在/usr/include/c++/4.9/bits/stl_vector.h:676
    B :: addA中的#9 0x000000000041e3d5(这= 0x7ffff268cb60,         id = 19002,e = ...)在filesrc.h:143

  

或者这个错误。

  __GI_raise中的

#0 0x00007ffff620f067(sig = sig @ entry = 6)       在../nptl/sysdeps/unix/sysv/linux/raise.c:56
  abort.c:89的__GI_abort()中的#1 0x00007ffff6210448   __libc_message中的#2 0x00007ffff624d1b4(do_abort = do_abort @ entry = 1,       fmt = fmt @ entry = 0x7ffff6342210&#34; *错误%s&#39;:%s:0x%s * \ n&#34;)       at ../sysdeps/posix/libc_fatal.c:175
  malloc_printerr中的#3 0x00007ffff625298e(action = 1,       str = 0x7ffff6342318&#34;双重免费或腐败(!prev)&#34;,       ptr =)在malloc.c:4996
  _int_free中的#4 0x00007ffff6253696(av =,p =,       have_lock = 0)在malloc.c:3840
  A ::〜A中的#5 0x000000000041f598(这= 0x7fffc032d410,       __in_chrg =)在objrecipe.h:23
  #6 0x000000000042dd9e在std :: _ Destroy中(       __pointer = 0x7fffc032d410)/usr/include/c++/4.9/bits/stl_construct.h:93
  #7 0x000000000042b0e9在std :: _ Destroy_aux :: __ destroy(       __first = 0x7fffc032d410,__last = 0x7fffc0344920)       在/usr/include/c++/4.9/bits/stl_construct.h:103
  #8 0x00000000004275a1在std :: _ Destroy中(       __first = 0x7fffc02f28a0,__last = 0x7fffc0344920)       在/usr/include/c++/4.9/bits/stl_construct.h:126
  #9 0x00000000004236b7 in std :: _ Destroy(       __first = 0x7fffc02f28a0,__last = 0x7fffc0344920)       在/usr/include/c++/4.9/bits/stl_construct.h:151
  #10 0x000000000042380f in std :: vector&gt; :: _ M_default_append(this = 0x7ffff268cb78,__n = 1000)       在/usr/include/c++/4.9/bits/vector.tcc:576
  #11 0x000000000041fa6b in std :: vector&gt; :: resize(this = 0x7ffff268cb78,__ new_size = 21000)       在/usr/include/c++/4.9/bits/stl_vector.h:676
  B :: addA中的#12 0x000000000041e5ae(这= 0x7ffff268cb60,       id = 19001,e = ...)在filesrc.h:112

0 个答案:

没有答案