Point class

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>::point(T, T, AssertEqual<n, 2>&, int)'

Thanks for your help.
 
R

Rolf Magnus

aaragon said:
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>::point(T, T, AssertEqual<n, 2>&, int)'

Thanks for your help.

Are you sure you posted the whole file? If I count 34 lines from the
beginnig (you could really have marked the erroneous lines...), I get to
the line:

m_coordinate[1] = v1;

which doesn't seem to fit the error message.
 
S

Serge Paccalin

Le 27.03.2007 07:33, aaragon a ecrit:
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:

I had it compile by adding typedef's for the AssertEqual said:
template<int nDim>
class Point
{
public:
double GetCoordinate(int iDim)
{
return m_coordinate[iDim];
}

typedef AssertEqual<nDim,1> AssertEqual1;
typedef AssertEqual<nDim,2> AssertEqual2;
typedef AssertEqual said:
// 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>() )

Point( double v0, AssertEqual1 & = AssertEqual1() )

Same for the other constructors.
geom.h:34: error: default argument missing for parameter 4 of
'Point<n, T>::point(T, T, AssertEqual<n, 2>&, int)'

This doesn't match the code you gave.
 
A

aaragon

Le 27.03.2007 07:33, aaragon a ecrit:
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:

I had it compile by adding typedef's for the AssertEqual said:
template<int nDim>
class Point
{
public:
double GetCoordinate(int iDim)
{
return m_coordinate[iDim];
}

typedef AssertEqual<nDim,1> AssertEqual1;
typedef AssertEqual<nDim,2> AssertEqual2;
typedef AssertEqual said:
// 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>() )

Point( double v0, AssertEqual1 & = AssertEqual1() )

Same for the other constructors.
geom.h:34: error: default argument missing for parameter 4 of
'Point<n, T>::point(T, T, AssertEqual<n, 2>&, int)'

This doesn't match the code you gave.

Well, sorry about the line numbering, I just copied the code in other
file that I had. Line 34 corresponds to the definition of the second
constructor (and I commented the other two). I tried the typedef and
it compiles as long as I don't put the & symbol after AssertEqual1, as
follows:

typedef AssertEqual<n,2> AssertEqual2;

Point(T x, T y, AssertEqual2 = AssertEqual2() )

What is happening here? Can anyone explain why it compiles here and
not having the following line?

Point(T x, T y, AssertEqual2& = AssertEqual2() )

giving the following error

aaragon@linux-aguila:~/Desktop/various/point class> g++ geom.cxx
geom.cxx: In function 'int main()':
geom.cxx:8: error: default argument for parameter of type
'AssertEqual<2, 2>&' has type 'AssertEqual<2, 2>'
[2]+ Done emacs geom.cxx

In this case, line 8 of geom.cxx refers to the definition of a point:

Point<2> p(0.1,0.4);

Thank you.
 
R

Rolf Magnus

aaragon said:
Well, sorry about the line numbering, I just copied the code in other
file that I had. Line 34 corresponds to the definition of the second
constructor (and I commented the other two). I tried the typedef and
it compiles as long as I don't put the & symbol after AssertEqual1, as
follows:

typedef AssertEqual<n,2> AssertEqual2;

Point(T x, T y, AssertEqual2 = AssertEqual2() )

What is happening here? Can anyone explain why it compiles here and
not having the following line?

Point(T x, T y, AssertEqual2& = AssertEqual2() )

You're trying to bind a non-const reference to a temporary, which is not
allowed in standard C++.
 
A

aaragon

You're trying to bind a non-const reference to a temporary, which is not
allowed in standard C++.

Hi everyone, I was able to compile the code but I did something
somewhat different. This is what I do:


template<bool> struct ConstAssert;
template<> struct ConstAssert<true> {};

template<int n, typename T = double>
class Point
{
// coordinates storage
T coord[n];

public:

static int const dim = n;

typedef T value_type;
typedef T* pointer;
typedef T& reference;

// the constructors' definition adds a default last parameter
whose type
// will have an error if it is instantiated with the a false
boolean
// 2D point constructor
Point(T x, T y,ConstAssert<n == 2> = ConstAssert<true>())
{
coord[0] = x;
coord[1] = y;
}

// 3D point constructor
Point(T x, T y, T z, ConstAssert<n == 3> = ConstAssert<true>())
{
coord[0] = x;
coord[1] = y;
coord[2] = z;
}
};

Then, since the ConstAssert is not defined for a false template
parameter, it doesn't compile. Now, the problem is that the error
message doesn't give a single hint about what's going on, except for
the name of the object ConstAssert:

aaragon@linux-aguila:~/Desktop/various/point class> g++ geom.cxx
geom.cxx: In function 'int main()':
geom.cxx:2: error: default argument for parameter of type
'ConstAssert<false>' has type 'ConstAssert<true>'
geom.h: In constructor 'Point<n, T>::point(T, T, ConstAssert<(n ==
2)>) [with int n = 4, T = double]':
geom.cxx:2: instantiated from here
geom.h:33: error: '<anonymous>' has incomplete type
geom.h:9: error: declaration of 'struct ConstAssert<false>'

Does anyone have an idea of how to improve this so the user of this
code can understand what's going on?
 
O

Old Wolf

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?

template <int L1, int L2 >
struct AssertEqual
{
char foo[ (L1==L2)?1:-1 ]; // error if L1 != L2
};

template<int nDim>
class Point
{
public:
Point( double v0, AssertEqual<nDim,1> & = AssertEqual<nDim,1>() )
{
m_coordinate[0] = v0;
}

You can't bind a temporary to a non-const reference.
Anyway, couldn't you achieve the same thing with:

Point( double v0 )
{
AssertEqual<nDim, 1>();
m_coordinate[0] = v0;
}
 

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
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top