Tangled up in templates

M

Martin Magnusson

I have gotten myself all tangled up in templated classes and can't quite
see what to do right now. GCC 3.3.1 give me an error saying

test.cpp: In constructor `Vertex<N>::Vertex(A::Vector<N, float>) [with
int N = 3]':
test.cpp:55: error: invalid use of `template<int N, class T> class
A::Vector'

I suspect that it has something to do with the default argument of
Vertex's constructor. It works better if I define a default constructor
without arguments.

I hope that someone of you has the time to take a look at this and see
if you can find what's wrong. I've tried to cut the code below down to a
minimum.

/// Begin file test.cpp //////////////////////////////////////////////
#include <iostream>
using namespace std;

namespace A
{
template< int N, typename T >
class Vector
{
public:
Vector( int value )
{
i = value + N;
}

int i;
};

typedef Vector< 3, float > Vector3d;

template< int N, typename T >
class Compare
{
public:
bool operator () ( const Vector< N, T >& lhs, const Vector< N, T >&
rhs )
{
return lhs.i < rhs.i;
}
};
}

template< int N >
class Vertex
{
public:
Vertex( A::Vector<N,float> position = A::Vector( 0 ) );//Something
wrong here?

class Compare
{
public:
bool operator () ( const Vertex< N >& lhs, const Vertex< N >& rhs )
{
return A::Compare< N, float >( lhs.v, rhs.v );
}
};

A::Vector<N,float> v;

};

typedef Vertex<3> Vertex3d;

int main()
{
Vertex<3> vert1;
cout << vert1.v.i; // Error on this line!

return 0;
}


/// End file test.cpp ////////////////////////////////////////////////
 
M

Martin Magnusson

Sorry, the error generated one line above where I marked in my previous
post. It should be like this:

int main()
{
Vertex<3> vert1; // Error on this line!
cout << vert1.v.i;
return 0;
}
 
J

John Harrison

Martin Magnusson said:
I have gotten myself all tangled up in templated classes and can't quite
see what to do right now. GCC 3.3.1 give me an error saying

test.cpp: In constructor `Vertex<N>::Vertex(A::Vector<N, float>) [with
int N = 3]':
test.cpp:55: error: invalid use of `template<int N, class T> class
A::Vector'

I suspect that it has something to do with the default argument of
Vertex's constructor. It works better if I define a default constructor
without arguments.

I hope that someone of you has the time to take a look at this and see
if you can find what's wrong. I've tried to cut the code below down to a
minimum.

/// Begin file test.cpp //////////////////////////////////////////////
#include <iostream>
using namespace std;

namespace A
{
template< int N, typename T >
class Vector
{
public:
Vector( int value )
{
i = value + N;
}

int i;
};

typedef Vector< 3, float > Vector3d;

template< int N, typename T >
class Compare
{
public:
bool operator () ( const Vector< N, T >& lhs, const Vector< N, T >&
rhs )
{
return lhs.i < rhs.i;
}
};
}

template< int N >
class Vertex
{
public:
Vertex( A::Vector<N,float> position = A::Vector( 0 ) );//Something
wrong here?

Yes should be

Vertex( A::Vector<N,float> position = A::Vector<N, float>(
0 ) );//Something

john
 
M

Martin Magnusson

John said:
Yes should be

Vertex( A::Vector<N,float> position = A::Vector<N, float>(
0 ) );

OK, that would make sense, but it gives me a "syntax error before '>' token"

/ martin
 
J

John Harrison

Martin Magnusson said:
OK, that would make sense, but it gives me a "syntax error before '>' token"

/ martin

Seems to be a compiler bug, your code with my change compiles fine on
Comeau's online compiler (http://www.comeaucomputing.com/tryitout/). I
suggest you try splitting your constructor into two instead of using a
default parameter value.

john
 
S

Sharad Kala

Actually it compiles with g++ 3.3.1 if you put an extra pair of parenthesis,
not quite sure as to what the compiler is doing though.
Vertex( A::Vector<N,float> position = (A::Vector<N, float>(0) ) );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,150
Messages
2,570,853
Members
47,394
Latest member
Olekdev

Latest Threads

Top