Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么没有';移动构造函数没有调用吗? 我正在做一个C++入门第五版的练习,它的内容如下:_C++_C++11_Stl_Constructor_Move Constructor - Fatal编程技术网

为什么没有';移动构造函数没有调用吗? 我正在做一个C++入门第五版的练习,它的内容如下:

为什么没有';移动构造函数没有调用吗? 我正在做一个C++入门第五版的练习,它的内容如下:,c++,c++11,stl,constructor,move-constructor,C++,C++11,Stl,Constructor,Move Constructor,练习13.50:将打印语句放入 String根据§13.6.1中的练习13.48重新运行程序(第。 534)使用向量查看何时避免复制。(第544页) String是一个练习类,其行为类似于std::String,不使用任何模板。String.h文件: class String { public: //! default constructor String(); //! constructor taking C-style string i.e. a char arra

练习13.50:将打印语句放入 String根据§13.6.1中的练习13.48重新运行程序(第。 534)使用向量查看何时避免复制。(第544页)

String
是一个练习类,其行为类似于
std::String
,不使用任何模板。
String.h
文件:

class String
{
public:
    //! default constructor
    String();

    //! constructor taking C-style string i.e. a char array terminated with'\0'.
    explicit String(const char * const c);

    //! copy constructor
    explicit String(const String& s);

    //! move constructor    
    String(String&& s) noexcept;

    //! operator =
    String& operator = (const String& rhs);

    //! move operator =     
    String& operator = (String&& rhs) noexcept;

    //! destructor
    ~String();

    //! members
    char* begin() const  { return elements;   }
    char* end()   const  { return first_free; }

    std::size_t size()     const {return first_free - elements;  }
    std::size_t capacity() const {return cap - elements;         }

private:

    //! data members
    char* elements;
    char* first_free;
    char* cap;

    std::allocator<char> alloc;

    //! utillities for big 3
    void free();

};
Main.cpp

int main()
{
    std::vector<String> v;
    String s;
    for (unsigned i = 0; i != 4; ++i)
        v.push_back(s);

    return 0;
}
可以看出,根本没有调用move构造函数。当向量分配更多内存时,为什么不调用move构造函数

更新:

编译器信息:

gcc version 4.7.3 (Ubuntu/Linaro 4.7.3-1ubuntu1) 
main.cpp
具有打印能力及其输出:

int main()
{
    std::vector<String> v;
    String s;
    for (unsigned i = 0; i != 4; ++i)
    {
        std::cout << v.capacity() << "\n";
        v.push_back(s);
    }

    return 0;
}

我在MinGW上使用GCC4.7.1进行复制


添加
~String()noexcept
解决了这个问题…

,只要我添加了足够多的缺少的定义以供编译。您使用的是哪种编译器?检查向量的容量-可能它为前面的四个或更多元素保留空间?这是哪个编译器?在gcc中工作(4.7.2和4.8.2-只有我现在可以访问的…@MikeSeymour 4.73,我更新了线程,添加了容量打印。@Nim 4.73,我更新了线程,添加了容量打印。投票关闭它,问题将被标记为关闭,其他人也将投票关闭,然后问题将被关闭。但是不要删除它,因为关于这是一个gcc错误的信息在另一个问题中也不是很突出。
gcc version 4.7.3 (Ubuntu/Linaro 4.7.3-1ubuntu1) 
int main()
{
    std::vector<String> v;
    String s;
    for (unsigned i = 0; i != 4; ++i)
    {
        std::cout << v.capacity() << "\n";
        v.push_back(s);
    }

    return 0;
}
0
Copy constructing......
1
Copy constructing......
Copy constructing......
2
Copy constructing......
Copy constructing......
Copy constructing......
4
Copy constructing......