拆分循环相关的CRTP类标头时,函数调用不匹配

问题描述

因此,我有一个用于矩阵的CRTP类(在本主题中仍然很新),它带有一个Matrix_Base一个派生的Dynamic_Matrix类(以及一个Static_Matrix派生类)。最少的示例包括

Matrix_Base在cml.h中定义:

#ifndef _CML_H_
#define _CML_H_

#include <assert.h>
#include <math.h>
#include "dynamic_matrix.h"

namespace CML
{
    template<class T,class Derived>
    class Matrix_Base
    {
    public:

        inline T& operator()(const size_t row,const size_t col) 
        {
            Derived& self = get_this();
            assert(row < self.get_rows() && col < self.get_cols()); 

            return (T&) self.get_data()[self.get_cols() * row + col]; 
        }

        inline Derived& get_this() const { return (Derived&)*this; }

        friend inline std::ostream& operator<<(std::ostream& os,const Derived &other)
        {
            for (size_t i = 0; i < other.get_rows(); i++)
            {
                for (size_t j = 0; j< other.get_cols(); j++)
                {
                    os << other(i,j) << ' ';  // The operator()(..) call here then gives me error on "no match for call to ..." when using Derived = Dynamic_Matrix<T>
                }
                os << '\n';
            }
            return os;
        }
    };
}

以及Dynamic_Matrix中派生的dynamic_matrix.h类:

#ifndef _DYNAMIC_MATRIX_H_
#define _DYNAMIC_MATRIX_H_

#include <assert.h>
#include <math.h>

namespace CML
{
    template<class T,class Derived> class Matrix_Base;
    template <class T>
    class Dynamic_Matrix : public Matrix_Base<T,Dynamic_Matrix<T>> 
    {
    private:
        size_t n_rows,n_cols;

        T* data;

        void allocate_data() { if (data == nullptr) { data = new T[n_rows * n_cols];} }

        void deallocate_data() { if (data != nullptr) { delete[] data; data = nullptr;}}

    public:

        Dynamic_Matrix(const size_t n_rows,const size_t n_cols) : n_rows(n_rows),n_cols(n_cols),data(nullptr) { allocate_data(); }

        ~Dynamic_Matrix() { deallocate_data(); }

        inline size_t get_rows() const { return n_rows; }

        inline size_t get_cols() const { return n_cols; }

        inline T* get_data() const { return data; }
        
    };
}

运行简单代码

#include "cml.h"
#include <iostream>
int main()
{
     CML::Dynamic_Matrix<double> test(3,3);
     std::cout << test << std::endl;
}

然后给出error: no match for call to ‘(const CML::Dynamic_Matrix<double>) (size_t&,size_t&)’ os << other(i,j) << ' '; 是由于operator()(const size_t,const size_t)类中定义了Matrix_Base

这是什么问题?

解决方法

在编译器消息中,我也收到此消息。

note:   passing 'const CML::Dynamic_Matrix<double>*' as 'this' argument discards qualifiers

因此这意味着它与模板或CRTP无关,只是other是const引用,而operator()未标记为const。因此,只需在其上放置const限定词即可。

        inline T& operator()(const size_t row,const size_t col) const;

或将此函数的other参数设为非常量。

        friend inline std::ostream& operator<<(std::ostream& os,Derived &other)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...