0

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

11
  • 1
    Have you included the header for that template? Commented Jul 23, 2009 at 8:52
  • yes, I have, I have forgotten to include it in the question here.sorry Commented Jul 23, 2009 at 8:54
  • Please make the title of the question more descriptive. "use of templates" gives barely any idea of what it's about. Suggestion: "Confusing error when calling C++ template function" Commented Jul 23, 2009 at 8:56
  • 1
    May it be that the template in the header is declared inside a namespace? Commented Jul 23, 2009 at 8:56
  • 1
    posting the actual code using copy & paste rather than typing in an approximation would be a good idea Commented Jul 23, 2009 at 9:48

3 Answers 3

1

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

#define MxPoly matrsde<double>
Sign up to request clarification or add additional context in comments.

3 Comments

I used typedef matrdse< polynomial_t> MxPoly; sorry for mistyping.
is polynomial_t the same as double? If not, there's your problem.
yes indeed this was the error. I need typedef matrdse<double> MxDouble; for the template used with this double matrix not a matrix of polynomials indeed.
0

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;

2 Comments

Talk about having the same answer! :-)
The macro #define matrdse<double> MxPoly; tells the preprocessor to replace the token matrdse with the tokens <double> MxPoly;
0

#define matrdse<double> MxPoly;

one semicolon to much

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.