madalina
madalina

Reputation: 377

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". There is also defined a structure called "matrsdse" in a file "matrdse.hpp" which represents a dense matrix type and which has several

-constructors,e.g matrdse(int nrRows, int nrCols, const value_type *src, ByRow() )

-and methods, e.g. transpose().

I want to use the "horizontal_join" template in my main function:

#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:

 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

Upvotes: 0

Views: 239

Answers (3)

peterchen
peterchen

Reputation: 41116

#define matrdse<double> MxPoly;

one semicolon to much

Upvotes: 0

Ari
Ari

Reputation: 3500

Is this the actual code? You have the #define (a bad idea in itself) reversed. It should be

#define MxPoly matrsde<double>

Upvotes: 1

1800 INFORMATION
1800 INFORMATION

Reputation: 135443

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 matrdse<double> MxPoly;

Upvotes: 0

Related Questions