A
aaragon
Hi everyone, I found an example of a Point class in another message in
the group but I can't compile with gnu g++. Anyone has any clue why it
doesn't work? The code for the class and the error messages are:
// This template will have an error if L1 & L2 are not equal
template <int L1, int L2 >
struct AssertEqual
{
char foo[ (L1==L2)?1:-1 ]; // error if L1 != L2
};
template<int nDim>
class Point
{
public:
double GetCoordinate(int iDim)
{
return m_coordinate[iDim];
}
// define all the constuctors adding a default last parameter
// whose type will have an error if it is instantiated
Point( double v0, AssertEqual<nDim,1> & = AssertEqual<nDim,1>() )
{
m_coordinate[0] = v0;
}
Point( double v0, double v1, AssertEqual<nDim,2> & =
AssertEqual<nDim,2>() )
{
m_coordinate[0] = v0;
m_coordinate[1] = v1;
}
Point( double v0, double v1, double v2, AssertEqual<nDim,3> & =
AssertEqual<nDim,3>() )
{
m_coordinate[0] = v0;
m_coordinate[1] = v1;
m_coordinate[2] = v2;
}
private:
double m_coordinate[nDim];
};
aa@linux:~/Desktop> g++ geom.cxx
geom.h:34: error: expected identifier before numeric constant
geom.h:34: error: expected ',' or '...' before numeric constant
geom.h:34: error: wrong number of template arguments (1, should be 2)
geom.h:12: error: provided for 'template<int a, int b> struct
AssertEqual'
geom.h:34: error: default argument missing for parameter 4 of
'Point<n, T>:oint(T, T, AssertEqual<n, 2>&, int)'
Thanks for your help.
the group but I can't compile with gnu g++. Anyone has any clue why it
doesn't work? The code for the class and the error messages are:
// This template will have an error if L1 & L2 are not equal
template <int L1, int L2 >
struct AssertEqual
{
char foo[ (L1==L2)?1:-1 ]; // error if L1 != L2
};
template<int nDim>
class Point
{
public:
double GetCoordinate(int iDim)
{
return m_coordinate[iDim];
}
// define all the constuctors adding a default last parameter
// whose type will have an error if it is instantiated
Point( double v0, AssertEqual<nDim,1> & = AssertEqual<nDim,1>() )
{
m_coordinate[0] = v0;
}
Point( double v0, double v1, AssertEqual<nDim,2> & =
AssertEqual<nDim,2>() )
{
m_coordinate[0] = v0;
m_coordinate[1] = v1;
}
Point( double v0, double v1, double v2, AssertEqual<nDim,3> & =
AssertEqual<nDim,3>() )
{
m_coordinate[0] = v0;
m_coordinate[1] = v1;
m_coordinate[2] = v2;
}
private:
double m_coordinate[nDim];
};
aa@linux:~/Desktop> g++ geom.cxx
geom.h:34: error: expected identifier before numeric constant
geom.h:34: error: expected ',' or '...' before numeric constant
geom.h:34: error: wrong number of template arguments (1, should be 2)
geom.h:12: error: provided for 'template<int a, int b> struct
AssertEqual'
geom.h:34: error: default argument missing for parameter 4 of
'Point<n, T>:oint(T, T, AssertEqual<n, 2>&, int)'
Thanks for your help.