【问题标题】:std::copy: operator= not foundstd::copy: 运算符= 未找到
【发布时间】:2016-08-14 09:05:09
【问题描述】:

在 Vector_t 的 initializer_list 构造函数上使用 std::copy 时遇到问题。但正如您在 FixedPoint_t 的声明中看到的那样,它有足够的声明用于复制构造和赋值。编译器对我的期望是什么签名?或者我在这里错过了什么?编译器输出指出 FixedPoint_t 的 operator= 可能适合,但仍未使用。匹配参数列表似乎也存在问题。

我尝试过的: Vector_t适用于整数类型和其他类,所以它必须是FixedPoint_t的一些问题。
MSDN 页面表示缺少构造函数。但 FixedPoint_t 的 ctor 匹配。
FixedPoint_t 可以在 Vector_t 之外分配,但我无法得出结论。
我无法用我制作的 int-wrapper 重现错误。

编译器:VS 编译器(VS 2015)
错误:C2679 二元运算符“=”:未找到类型为“const math::FixedPoint_t”的 rhs 参数的运算符(或无法进行适当的转换)

测试代码

#include <FpMath/FpMath.hpp>

using namespace math;

int main(int argc, char** argv) {

    // Vector object construction using an initializer_list
    Vector_t<FixedPoint_t<int, 4096>, 1> vec {
        4096
    };

    // Assignment outisde of Vector_t
    FixedPoint_t<int, 4096> fp1(3 * 4096);
    FixedPoint_t<int, 4096> fp2 = fp1; // works

    return 0;
}

矢量.hpp

#pragma once

#include <array>
#include <cassert>
#include <initializer_list>
#include <algortihm> // std::copy

#include "FixedPoint.hpp"

namespace math {

    template <typename T, size_type size>
    class Vector_t {
    public:

        typedef T value_type;

        Vector_t(std::initializer_list<value_type> li);

    ...

    private:

        std::array<value_type, size> m_values;
    };

    template <typename T, size_type size>
    Vector_t<T, size>::Vector_t(std::initializer_list<value_type> li) : m_values() {

        assert(li.size() <= size);
        std::copy(li.begin(), li.end(), m_values.begin()); // < Error occurs here
    }
}

FixedPoint.hpp

#pragma once

#include <cassert>
#include <limits>

namespace math {

    template <typename T, T denom>
    class FixedPoint_t {
    public:

        typedef T value_type;
        typedef class_type& reference;

        FixedPoint_t();
        FixedPoint_t(const value_type& numerator);
        FixedPoint_t(const reference other);

        inline reference operator=(const reference other);
        inline reference operator=(const value_type& val);
    };
}

【问题讨论】:

  • 致反对者:您能否就如何改进我的问题发表评论。谢谢。
  • 也许他们投了反对票,因为这不是一个最小的例子。开始删除不必要的方法,typedefs,......所有不需要的东西。当您有绝对最小值时,请在此处发布。您很可能还会找到问题的根源,在这种情况下,您也可以发布解决方案。
  • 并且以防万一更改您的 MSDN 链接,以便它可以打开英文页面。这里有一定数量的人不读德语,也不想打扰翻译选项
  • 另外,除了最小化之外,示例还必须完整。我们必须能够复制源代码、构建它并重现错误。通过省略 FixedPoint.tpp 您可以阻止我们这样做。如果它太大,它不是一个最小的例子。
  • const reference != const class_type&amp;。 (这就是为什么前缀-const 会产生误导。我建议使用后缀-const。)

标签: c++ templates c++11 operator-overloading


【解决方案1】:

问题出在这里:

typedef class_type& reference;
...
FixedPoint_t(const reference other);
...
inline reference operator=(const reference other);

此构造不声明 const 引用参数。将其更改为

typedef class_type& reference;
typedef const class_type& const_reference;
...
FixedPoint_t(const_reference other);
...
inline reference operator=(const_reference other);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2015-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-18
  • 1970-01-01
  • 2010-10-12
  • 1970-01-01
相关资源
最近更新 更多