简体   繁体   English

模板参数变量/动态实例化

[英]Template Parameter variable / Dynamic Instantiation

I am an absolutely beginner in C++, so I really would appreciate your help! 我绝对是C ++的初学者,因此,非常感谢您的帮助!

I am currently implementing a Matrix Class with Template Parameters for datatype, dimension width and dimension height. 我目前正在使用带有用于数据类型,尺寸宽度和尺寸高度的模板参数的Matrix类。

template <class T, int rows, int columns> class Matrix

In the multiplication function I have to create the result matrix (Height Matrix A x Width Matrix B), but I receive the error "template value cannot appear in a constant expression". 在乘法函数中,我必须创建结果矩阵(高度矩阵A x宽度矩阵B),但是收到错误“模板值不能出现在常量表达式中”。

// overload * for matrix multiplication 
   template <class T, int rows, int columns>
   Matrix<T, rows, columns> operator*(Matrix<T, rows, columns> a, Matrix<T, rows, columns> b) {
     Matrix <T, rows, columns> result = new Matrix<T, a->height, b->width>;
// make multiplication here

Any ideas how to make a new Matrix with the given type and the height from A resp. 任何想法如何用给定的类型和高度分别创建一个新的矩阵。 width from B? B的宽度?

Thank you so much! 非常感谢!

How about make rows and columns as the parameters of the constructors? 如何将行和列作为构造函数的参数?

template <class T> class Matrix
{        
public:           
   Matrix(int rows, int columns)
   {....}    
}

//then you can create a new instance with: //然后您可以使用以下命令创建一个新实例:

 Matrix <T>* result = new Matrix<T> (a->height, b->width)

Try to think of two different concepts when referring to your template class Matrix that stores the data of the matrix and the *operator which is a template method. 当引用存储矩阵数据的模板类Matrix和作为模板方法的* operator时,请尝试考虑两个不同的概念。

If you think of the *operator as a template method that multiplies template classes, then you realize that you need more template arguments and that those arguments are only partial given by one instance of the matrix class. 如果您将* operator视为乘以模板类的模板方法,那么您会意识到需要更多的模板参数,并且这些参数仅是由矩阵类的一个实例部分给出的。

The error is given because a->width and a->height a not know at compile time and every template argument has to be known at compile time. 给出错误是因为在编译时a-> width和a-> height a不知道,并且每个模板参数都必须在编译时知道。

As the error says, you cannot set static (compile time) "fields" from dynamic (run time) values. 如错误所述,您不能从动态(运行时)值设置静态(编译时)“字段”。

You could ( not saying you should ) use different template parameters for the sizes of your two matrices and create a new matrix with them : 您可以( 不是说应该 )对两个矩阵的大小使用不同的模板参数,并使用它们创建一个新的矩阵:

template <class T, int rows_A, int columns_A, int rows_B, int columns_B>
Matrix<T, rows_A, columns_B> operator*(Matrix<T, rows_A, columns_A> a, Matrix<T, rows_B, columns_B> b) {
    Matrix <T, rows_A, columns_B> result;
    // ...
    return result;
}

And as Joe Z. says, you'll also have to check the validity of the operation before. 正如Joe Z.所说,您还必须在此之前检查操作的有效性。 Because columns_A must be equal to rows_B , you can factorize the two template parameters. 因为columns_A必须等于rows_B ,所以可以分解两个模板参数。

EDIT : mmmmmmm wrote this factorization in below comments : 编辑:mmmmmmm在下面的评论中写道:

template <class T, int rows_A, int columns_A_rows_B, int columns_B>
Matrix<T, rows_A, columns_B> operator*(
    Matrix<T, rows_A, columns_A_rows_B> a,
    Matrix<T, columns_A_rows_B, columns_B> b
) {
    Matrix <T, rows_A, columns_B> result;
    // ...
    return result;
}

Yet ,this code is not really good looking, neither easy to use. 然而,这段代码看起来并不好看,也不易于使用。 If you can, you could try the idea of making the sizes dynamic (not parameter templates), as suggested by Matt. 如果可以的话,您可以尝试按照Matt的建议使尺寸动态化(而不是参数模板)。

It is looks like in this line: 看起来像这一行:

Matrix <T, rows, columns> result = new Matrix<T, a->height, b->width>;

Is error. 是错误的。 You are trying to write new Address to variable which is declared on stack. 您正在尝试将新的地址写入在堆栈中声明的变量。 Try this: 尝试这个:

Matrix<T, int, int> *result = new Matrix<T, a->height, b->width>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM