[英]Problem compiling C++ template code
I have the following template: 我有以下模板:
template<class Matrix>
Matrix horizontal_join (const Matrix& m1, const Matrix& m2) {
ASSERT (rows (m1) == rows (m2), "unequal number of rows");
typedef typename Matrix::value_type Scalar;
Matrix r (rows (m1), nbcol (m1) + nbcol (m2));
for (unsigned i=0; i<nbrow(m1); i++) {
for (unsigned j=0; j<nbcol(m1); j++)
r(i,j)= m1(i,j);
for (unsigned j=0; j<nbcol(m2); j++)
r(i,j+nbcol(m1))= m2(i,j);
}
return r;
}
defined in some library called "MATRDSE.m". 在某些名为“ MATRDSE.m”的库中定义。 There is also defined a structure called "matrsdse" in a file "matrdse.hpp" which represents a dense matrix type and which has several
在文件“ matrdse.hpp”中还定义了一个名为“ matrsdse”的结构,该结构表示密集的矩阵类型,并且具有多个
-constructors,eg matrdse(int nrRows, int nrCols, const value_type *src, ByRow() ) -构造函数,例如matrdse(int nrRows,int nrCols,const value_type * src,ByRow())
-and methods, eg transpose(). -和方法,例如transpose()。
I want to use the "horizontal_join" template in my main function: 我想在我的主要功能中使用“ horizontal_join”模板:
#include <matrdse.hpp>
#include <MATRDSE.m>
typedef matrdse<double> MxPoly;
int main{
double v[]={1,2,1,1,3,4,2,2,5,5,5,5,-2, 1,2,3,1, -2.8,-2.4,1,.2,5.8};
matrdse<double> A(4,4,v,ByRow());
std::cout<<"A="<<endl<<A<<endl;
matrdse<double> AT(A);
AT.transpose(); std::cout<<"Transpose(A)="<<endl<<AT<<endl;
MxPoly B;
B=horizontal_join<MxPoly>(A,AT);
cout<<B<<endl;
return 0;
}
Everything works fine, until "horizontal_join" is called and returned in B. I get the following compilation error: 一切正常,直到在B中调用“ horizontal_join”并返回为止。我收到以下编译错误:
main.cpp:168: error: 'horizontal_join' was not declared in this scope
main.cpp:168: error: expected primary-expression before '>' token
I do not understand the error. 我不明白该错误。 As I see it I do not know how to call the template..
据我所知,我不知道如何调用模板。
Thank you in advance for any suggestions, madalina 预先感谢您的任何建议,madalina
Is this the actual code? 这是实际的代码吗? You have the
#define
(a bad idea in itself) reversed. 您已经颠倒了
#define
(本身是个坏主意)。 It should be 它应该是
#define MxPoly matrsde<double>
I guess the problem is the combination of your preprocessor macro here (is this even right, do you have the name of the macro before the type?): 我猜问题出在这里是您的预处理器宏的组合(这是否正确,在类型之前您是否拥有宏的名称?):
#define matrdse<double> MxPoly;
and the template instantiation here: 以及此处的模板实例化:
B=horizontal_join<MxPoly>(A,AT);
Note that the instantiation line expands in the preprocessor to this: 请注意,实例化行在预处理器中扩展为:
B=horizontal_join<matrdse<double>>(A,AT);
The template-template thing ending in >>
is a well known compiler error that is masked here by the macro. 以
>>
结尾的模板模板是一个众所周知的编译器错误,在此被宏掩盖了。 I guess if you change the template instantiation line to this it goes away: 我想如果您将模板实例化行更改为此,它将消失:
B=horizontal_join<MxPoly >(A,AT);
I suggest you need to use some more consistent naming, and favour typedef over macro as it will not have this problem: 我建议您需要使用一些更一致的命名,并且优先使用typedef而不是宏,因为它不会出现此问题:
typedef matrdse<double> MxPoly;
#define matrdse<double> MxPoly;
一分号
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.