c++ - 拆分循环依赖的 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)
推荐阅读
- python - 导入多个略有不同的 excel 文件
- css - Fullcalendar 事件具有去饱和的背景颜色
- android - 如何修复安卓 6.0.1 的小米红米 3S 中不允许明文流量的问题
- arm - 如何使用 SIMD 优化 for 循环的计算?
- python - 使用列名字典来决定如何通过python中的方程相乘
- pandas - 如何合并两个熊猫数据框/系列
- regex - 我可以使用 BuckleScript 中的 OCaml 正则表达式吗?(特别是 Str.search_forward)
- php - 如何在 YAML 中输出整数浮点数?
- java - 为什么 infinispan 复制缓存不复制?
- asp.net-mvc - System.ArgumentNullException:值不能为空。参数名称:entitySet?