【问题标题】:Mix concrete and non-concrete types in templates C++在模板 C++ 中混合具体和非具体类型
【发布时间】:2015-11-20 13:51:15
【问题描述】:

我的问题有点像模板类 C++ 中的部分规范。

我写了一个template<class T> class matrix 来进行一些矩阵计算。对于这个类,我定义了重载的比较运算符,例如 ==、>=、

matrix<float> a(2,3), b(2,3);
a.fill(1); b.fill(2);
matrix<bool> c = (a == b);

导致布尔矩阵 c 具有相同的大小 abc(i,j)&lt;-true iff a(i,j) == b(i,j) 。使用以下代码无法做到这一点

template<class T>
class matrix
{
public:
    matrix ();
    matrix (size_t rows, size_t cols);
    matrix (const matrix<T>& mx);

    size_t rows() const;
    size_t cols() const;
    size_t size() const;

    const T* ptr() const;
    T* mutable_ptr();

    // etc
private:
    size_t num_rows;
    size_t num_cols;
    vector<T> data;
}

template<class T> const T* matrix<T>::ptr() const { return data.data(); }
template<class T> T* matrix<T>::mutable_ptr() { return data.data(); } // line **459**

template<class T> matrix<bool>&  operator< (const matrix<T>& mx, const T t);
template<class T> matrix<bool>&  operator<= (const matrix<T>& mx, const T t);
template<class T> matrix<bool>&  operator> (const matrix<T>& mx, const T t);
template<class T> matrix<bool>&  operator>= (const matrix<T>& mx, const T t);
template<class T> matrix<bool>&  operator== (const matrix<T>& mx, const T t);
template<class T> matrix<bool>&  operator!= (const matrix<T>& mx, const T t);
// ...
template<class T> matrix<bool>&  operator== (const matrix<T>& mx, const T value) { // line **547**
    matrix<bool> *index = new matrix<bool>(mx.rows(), mx.cols());
    const T *ptr = mx.ptr();
    bool *ptr_i = index->mutable_ptr();
    size_t m = mx.size();
    for (size_t i=0; i < m; ++i) {
        if (ptr[i] == value)
            ptr_i[i] = true;
        else
            ptr_i[i] = false;
    }
    return *index;
}

编译器输出

matrix.hpp: In member function ‘T* matrix<T>::mutable_ptr() [with T = bool]’:
matrix.hpp:547:35:   instantiated from ‘matrix<bool>& operator==(const matrix<T>&, T) [with T = float]’
compute_sil.cpp:163:26:   instantiated from here
matrix.hpp:459:66: error: void value not ignored as it ought to be

有什么办法可以克服吗?

编辑 抱歉,代码不够多,我确实在矩阵与标量之间以及矩阵之间重载了具有许多参数类型的运算符。

template<class T> matrix<bool>&  operator< (const matrix<T>& mx, const matrix<T>& mx2);
template<class T> matrix<bool>&  operator<= (const matrix<T>& mx, const matrix<T>& mx2);
template<class T> matrix<bool>&  operator> (const matrix<T>& mx, const matrix<T>& mx2);
template<class T> matrix<bool>&  operator>= (const matrix<T>& mx, const matrix<T>& mx2);
template<class T> matrix<bool>&  operator== (const matrix<T>& mx, const matrix<T>& mx2);
template<class T> matrix<bool>&  operator!= (const matrix<T>& mx, const matrix<T>& mx2);

template<class T> matrix<bool>&  operator< (const T t, const matrix<T>& mx);
template<class T> matrix<bool>&  operator<= (const T t, const matrix<T>& mx);
template<class T> matrix<bool>&  operator> (const T t, const matrix<T>& mx);
template<class T> matrix<bool>&  operator>= (const T t, const matrix<T>& mx);
template<class T> matrix<bool>&  operator== (const T t, const matrix<T>& mx);
template<class T> matrix<bool>&  operator!= (const T t, const matrix<T>& mx);

【问题讨论】:

  • 与您的问题无关,但您的相等运算符中存在内存泄漏。
  • @pateheo 您将const T 作为第二个参数,而不是const matrix&lt;T&gt;&amp;。决定你想要哪一个。
  • 哦,你展示的那个例子,比较两个 matrix 对象,不会调用你展示给我们的相等运算符(或给出你展示的错误)。
  • 也许比我最初建议的更好 - 重复将是这样的:stackoverflow.com/q/16568986/2079303

标签: c++ templates


【解决方案1】:

正如@user2079303 所评论的,问题是由于我使用了std::vector&lt;bool&gt;,与模板c++ 无关。 vector&lt;bool&gt; 不提供指向其数据的指针。我从使用 bool 更改为 charint 以便我的程序编译得很好。谢谢@user2079303。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-14
    • 1970-01-01
    • 2019-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    相关资源
    最近更新 更多