首页 > 解决方案 > 拆分循环依赖的 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_Matrixdynamic_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

这里有什么问题?

标签: c++templatescrtp

解决方案


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

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

所以这意味着它与模板或 CRTP 无关,它只是otherconst 引用但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)

推荐阅读