gpt4 book ai didi

c++ - 我的模板类定义中有一些循环

转载 作者:行者123 更新时间:2023-11-28 08:28:40 28 4
gpt4 key购买 nike

我有一个矩阵类,它采用可用于访问它的行和列引用类型作为模板参数。同时,行和列类型被传递给它们所代表的矩阵类型。

有没有办法打破这种循环?以下是一些演示代码片段:

#include "MatrixConcreteType.h"
template <class MatrixType>
class rowType <MatrixType>
{...}

在矩阵文件中:

#include "VectorTypes.h"
template <class row_t, class col_t>
class Matrix
{...}

我想我可以尝试一个很好的外部实习生?

最佳答案

如果我正确地理解了您的问题,那么您正在尝试将 Matrix 定义为采用行类型和列类型的模板,然后将 rowType(我还假设 columnType)定义为矩阵类型的模板。我的大问题是:实际数据在哪里?在某些时候,我认为这个矩阵实际上应该分解为一组结构化的整数、 double 、字符或指向 bool 值的指针的反向迭代器等。那是哪里?

你的循环似乎是试图 build 太多建筑的症状。弄清楚你想在哪个类中实际存储数据,使该类的模板参数成为该数据的类型,然后使所有其他相关类的模板参数成为主类或数据类型。例如:

template <class DataType>
class Matrix{
//store the data in here: a DataType [], or vector<DataType>, or something
}

和:

template <class MatrixType>
class Row{
//has something which refers back to the original matrix of type MatrixType
}

template <class DataType>
class Row{
//has something which refers back to the original matrix of type Matrix<DataType>
}

我建议使用上面的第二个替代方案,因为它更容易在 Matrix 类中引用 Row 类,例如:

template <class DataType>
Row<DataType> Matrix::getRow(int index){
//return an instance of Row<DataType> containing the appropriate row
}

关于c++ - 我的模板类定义中有一些循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3095658/

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