c++ - 为什么我不能在我的复制构造函数中使用 std::copy?

标签 c++ arrays copy-constructor

我正在编写一个封装二维数组的类。这是复制构造函数。 (WIDTHHEIGHT 是编译时常量,这就是我认为它适合使用数组的原因。)

MyClass::MyClass(const MyClass &other)
{
    std::copy(
            &array[0][0], &array[0][0] + WIDTH*HEIGHT,
            &other.array[0][0]);
}

我使用的方法根据 this question 是正确的,并在我将原型(prototype)更改为 const & 而不是简单的按值传递之前工作。但是,我现在收到此编译器错误:

In file included from /usr/include/c++/4.8/bits/char_traits.h:39:0,
                 from /usr/include/c++/4.8/string:40,
                 from MyClass.hpp:4,
                 from MyClass.cpp:1:
/usr/include/c++/4.8/bits/stl_algobase.h: In instantiation of ‘_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = ArrayDataType*; _OI = const ArrayDataType*]’:
/usr/include/c++/4.8/bits/stl_algobase.h:428:38:   required from ‘_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = ArrayDataType*; _OI = const ArrayDataType*]’
/usr/include/c++/4.8/bits/stl_algobase.h:460:17:   required from ‘_OI std::copy(_II, _II, _OI) [with _II = ArrayDataType*; _OI = const ArrayDataType*]’
MyClass.cpp:17:28:   required from here
/usr/include/c++/4.8/bits/stl_algobase.h:390:70: error: no matching function for call to ‘std::__copy_move<false, true, std::random_access_iterator_tag>::__copy_m(ArrayDataType*&, ArrayDataType*&, const ArrayDataType*&)’
                        _Category>::__copy_m(__first, __last, __result);
                                                                      ^
/usr/include/c++/4.8/bits/stl_algobase.h:390:70: note: candidate is:
/usr/include/c++/4.8/bits/stl_algobase.h:368:9: note: template<class _Tp> static _Tp* std::__copy_move<_IsMove, true, std::random_access_iterator_tag>::__copy_m(const _Tp*, const _Tp*, _Tp*) [with _Tp = _Tp; bool _IsMove = false]
         __copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result)
         ^
/usr/include/c++/4.8/bits/stl_algobase.h:368:9: note:   template argument deduction/substitution failed:
/usr/include/c++/4.8/bits/stl_algobase.h:390:70: note:   deduced conflicting types for parameter ‘_Tp’ (‘ArrayDataType’ and ‘const ArrayDataType’)
                        _Category>::__copy_m(__first, __last, __result);

我假设 C++ 标准不会使 std::copy 以这种方式通过常量引用在复制构造函数中不可用,那么我在这里做错了什么?

最佳答案

这个日志

/usr/include/c++/4.8/bits/stl_algobase.h: In instantiation of ‘_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = ArrayDataType*; _OI = const ArrayDataType*]’:

下面几行很有趣。错误日志指向 __result,它是您传递的第三个参数的别名。

这是从以下代码行生成的。

std::copy(
        &array[0][0], &array[0][0] + WIDTH*HEIGHT,
        &other.array[0][0]);

拷贝声明为:

template< class InputIt, class OutputIt >
OutputIt copy( InputIt first, InputIt last, OutputIt d_first );

您正在尝试将 array 复制到 other.array 并且目的地是 const

您可以将语法更改为:

std::copy(
        &other.array[0][0], &other.array[0][0] + WIDTH*HEIGHT,
        &array[0][0]);

但我建议使用 std::array std::array 编写一个不那么令人困惑的语法:

array = other.array;

这种混淆来自于一些语言设计决策:

赋值运算符用作:

LHS = RHS;

现在为了保持相同的顺序,C 将其函数定义为:

strcpy(LHSstring, RHSstring);  /* LHSstring = RHSstring; Similar in memcpy etc */

但是 C++ STL 设计不同,具有以下结构:

SOME_FUNC(from_iterator, to_iterator, something...);
/* foreach, transform, sort etc */

所以下面的(虽然令人困惑)是类似的

memcpy(dest, src, len * sizeof dest[0]);
std::copy(src, src + len, dst);

关于c++ - 为什么我不能在我的复制构造函数中使用 std::copy?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29247760/

相关文章:

c++ - 为链表创建复制构造函数

c++ - 当元素不存在于您的类中时,您如何实现复制构造函数?

c++ - 我的编码有问题吗?

c++ - 在 C++ 中创建对象的动态数组、删除对象和释放内存

Java 用户输入数组仅捕获 3 个整数,而不是 5 个

php - 是否可以从 HTML 表单(复选框)发布多个 "values"?

c++ - 编译器为具有引用和常量成员的类生成的复制/赋值函数

c++ - 多次使用 [x, y, z, ...]-clause 语法,不应该允许 operator[] 接受多个参数吗?

c++ - 如何在 C++ 中使用 future 和 promise 从函数线程获取返回值

Javascript:长度方法有效吗?