const参考和虚拟模板继承
const reference and virtual template inheritance
我在这里发布,因为我不确定为什么会遇到这个问题。.加上我不知道该如何解决。
我的目标是构建具有矩阵的线性代数系统,并能够在行或COLS或子膜上检索参考。
我有一个纯粹的虚拟类,它可以定义函数并保持对元素虚拟的访问:
template <typename T, std::size_t M, std::size_t N>
class densebase
{
public:
using SubMat = submat<T,M-1,N-1,densebase>;
using ConstSubMat = const submat<T,M-1,N-1,const densebase>;
virtual T& coeff(std::size_t row, std::size_t col) = 0;
virtual T& coeff(std::size_t index) = 0;
virtual const T& coeff(std::size_t row, std::size_t col) const = 0;
virtual const T& coeff(std::size_t index) const = 0;
SubMat sub(std::size_t row, std::size_t col) { return SubMat(*this, row, col); }
ConstSubMat sub(std::size_t row, std::size_t col) const { return ConstSubMat(*this, row, col); }
};
然后,我有矩阵类,它处理数据并覆盖COEFF函数
template <typename T, std::size_t M, std::size_t N>
class mat : public densebase<T,M,N>
{
public:
// constructors..
T& coeff(std::size_t row, std::size_t col) { return data_[row*N + col]; }
T& coeff(std::size_t index) { return data_[index]; }
const T& coeff(std::size_t row, std::size_t col) const { return data_[row*N + col]; }
const T& coeff(std::size_t index) const { return data_[index]; }
private:
std::vector<T> data_;
};
最后,subpatrix类,该类将densebase上的引用作为参数,并以不同的方式覆盖coeff函数:
template <typename T, std::size_t M, std::size_t N, typename parent>
class submat : public densebase<T,M,N>
{
public:
submat(parent& m, std::size_t row, std::size_t col)
: ref_(m), row_(row), col_(col)
{
}
T& coeff(std::size_t row, std::size_t col) { return ref_(row + (row >= row_), col + (col >= col_)); }
T& coeff(std::size_t index) { return ref_(index + (index >= row_),
index + (index >= col_)); }
const T& coeff(std::size_t row, std::size_t col) const { return ref_(row + (row >= row_), col + (col >= col_)); }
const T& coeff(std::size_t index) const { return ref_(index + (index >= row_),
index + (index >= col_)); }
private:
parent& ref_;
std::size_t row_, col_;
};
当我尝试从常数矩阵检索子序列时,这里出现了问题,我得到了汇编错误" float&amp;'的引用的无效初始化"从类型" const float'的表达" 。我虽然COEFF总是会返回一个参考,因为它被定义了。
完整错误消息:
||=== Build: Debug in ImDev (compiler: GNU GCC Compiler) ===|
C:ImagineImaginedemosImDevmain.cc||In instantiation of 'T& submat<T, M, N, parent>::coeff(std::size_t, std::size_t) [with T = float; unsigned int M = 2u; unsigned int N = 2u; parent = const densebase<float, 3u, 3u>; std::size_t = unsigned int]':|
C:ImagineImaginedemosImDevmain.cc|206|required from here|
C:ImagineImaginedemosImDevmain.cc|90|error: invalid initialization of reference of type 'float&' from expression of type 'const float'|
C:ImagineImaginedemosImDevmain.cc||In instantiation of 'T& submat<T, M, N, parent>::coeff(std::size_t) [with T = float; unsigned int M = 2u; unsigned int N = 2u; parent = const densebase<float, 3u, 3u>; std::size_t = unsigned int]':|
C:ImagineImaginedemosImDevmain.cc|206|required from here|
C:ImagineImaginedemosImDevmain.cc|92|error: invalid initialization of reference of type 'float&' from expression of type 'const float'|
C:ImagineImaginedemosImDevmain.cc||In member function 'T& submat<T, M, N, parent>::coeff(std::size_t) [with T = float; unsigned int M = 2u; unsigned int N = 2u; parent = const densebase<float, 3u, 3u>; std::size_t = unsigned int]':|
C:ImagineImaginedemosImDevmain.cc|92|warning: control reaches end of non-void function [-Wreturn-type]|
C:ImagineImaginedemosImDevmain.cc||In member function 'T& submat<T, M, N, parent>::coeff(std::size_t, std::size_t) [with T = float; unsigned int M = 2u; unsigned int N = 2u; parent = const densebase<float, 3u, 3u>; std::size_t = unsigned int]':|
C:ImagineImaginedemosImDevmain.cc|90|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build failed: 2 error(s), 6 warning(s) (0 minute(s), 0 second(s)) ===|
我错过了什么?
using ConstSubMat = const submat<T, M - 1, N - 1, const densebase>;
应该是
using ConstSubMat = const submat<const T, M - 1, N - 1, const densebase>;
保留constness。
相关文章:
- 在模板基类中为继承类中的可选重写生成虚拟方法
- 大小虚拟继承中的派生类
- 删除C++继承中虚拟类成员的代码重复
- 重写虚拟函数和继承
- C++ 多级虚拟继承编译问题
- 如何正确获得虚拟继承?
- 编译时继承类中的隐式虚拟与显式虚拟
- 钻石继承虚拟成员铸造与指针
- CPP 继承虚拟方法解析顺序
- 关于C 接口(纯虚拟类)/多重继承/虚拟继承的设计Qustion
- 继承虚拟类C 的派生类
- 多个继承/虚拟函数
- 不能从具有虚拟继承(C++虚拟继承)的类派生
- C++多重继承+虚拟函数(-歧义)=怪异行为(也是函数指针)
- 为什么我不能继承虚拟基的构造函数?
- C++继承虚拟函数崩溃
- c++多重继承/虚拟继承
- 派生类的成员函数是否从基类继承虚拟性?
- 继承虚拟函数的类的boost::optional失败的原因
- c++继承虚拟函数