gpt4 book ai didi

c++ - const 引用和虚拟模板继承

转载 作者:行者123 更新时间:2023-11-27 22:58:32 30 4
gpt4 key购买 nike

我在这里发帖是因为我不确定为什么会遇到这个问题..另外我也不知道如何解决..

我的目标是构建一个包含矩阵的线性代数系统,并能够检索行、列或子矩阵的引用。

我有一个纯虚类,它定义函数并保持对元素虚拟的访问:

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); }
};

然后,我有了矩阵类,它处理数据并覆盖系数函数

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_;
};

最后,submatrix 类,它以 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_;
};

当我尝试从常量矩阵中检索子矩阵时,问题就来了,我得到了编译错误“invalid initialization of reference of type 'float&' from expression of type 'const float'” .我虽然 coeff 总是会返回一个引用,因为它被定义了..

完整的错误信息:

||=== Build: Debug in ImDev (compiler: GNU GCC Compiler) ===|
C:\Imagine\Imagine\demos\ImDev\main.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:\Imagine\Imagine\demos\ImDev\main.cc|206|required from here|
C:\Imagine\Imagine\demos\ImDev\main.cc|90|error: invalid initialization of reference of type 'float&' from expression of type 'const float'|

C:\Imagine\Imagine\demos\ImDev\main.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:\Imagine\Imagine\demos\ImDev\main.cc|206|required from here|

C:\Imagine\Imagine\demos\ImDev\main.cc|92|error: invalid initialization of reference of type 'float&' from expression of type 'const float'|

C:\Imagine\Imagine\demos\ImDev\main.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:\Imagine\Imagine\demos\ImDev\main.cc|92|warning: control reaches end of non-void function [-Wreturn-type]|

C:\Imagine\Imagine\demos\ImDev\main.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:\Imagine\Imagine\demos\ImDev\main.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>;

保持常量。

关于c++ - const 引用和虚拟模板继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30126823/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com