473,387 Members | 1,303 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Point class

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:~/Desktopg++ 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 bstruct
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.

Mar 27 '07 #1
6 3725
aaragon wrote:
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:~/Desktopg++ 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 bstruct
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.

Mar 27 '07 #2
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<nDim,N>:
template<int nDim>
class Point
{
public:
double GetCoordinate(int iDim)
{
return m_coordinate[iDim];
}
typedef AssertEqual<nDim,1AssertEqual1;
typedef AssertEqual<nDim,2AssertEqual2;
typedef AssertEqual<nDim,3AssertEqual3;
// 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.

--
Serge Paccalin
<se************@easyvisio.net>
Mar 27 '07 #3
On Mar 27, 2:54 am, Serge Paccalin <serge.pacca...@easyvisio.net>
wrote:
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<nDim,N>:
template<int nDim>
class Point
{
public:
double GetCoordinate(int iDim)
{
return m_coordinate[iDim];
}

typedef AssertEqual<nDim,1AssertEqual1;
typedef AssertEqual<nDim,2AssertEqual2;
typedef AssertEqual<nDim,3AssertEqual3;
// 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.

--
Serge Paccalin
<serge.pacca...@easyvisio.net>
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,2AssertEqual2;

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 classg++ 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<2p(0.1,0.4);

Thank you.

Mar 27 '07 #4
aaragon wrote:
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,2AssertEqual2;

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++.

Mar 27 '07 #5
On Mar 27, 9:14 am, Rolf Magnus <ramag...@t-online.dewrote:
aaragon wrote:
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,2AssertEqual2;
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++.
Hi everyone, I was able to compile the code but I did something
somewhat different. This is what I do:
template<boolstruct 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 classg++ 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?

Mar 27 '07 #6
On Mar 27, 5:33 pm, "aaragon" <alejandro.ara...@gmail.comwrote:
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;
}

Mar 27 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Roger Leigh | last post by:
Hello, I'm writing a fixed-precision floating point class, based on the ideas in the example fixed_pt class in the "Practical C++ Programming" book by Steve Oualline (O' Reilly). This uses a...
1
by: ypjofficial | last post by:
Dear All, According to OOPs , a base class pointer can to point to derived class object....call this as fact1 But somehow I am not comfortable while understanding this concept. The explanaition...
10
by: Bhan | last post by:
Using Ptr of derived class to point to base class and viceversa class base { .... } class derived : public base { .... }
3
by: Norvin Laudon | last post by:
Hi, How can I add a multiplication operator to an existing class ("Point")? <i.e.> Point newPoint = new Point(existingPoint.X * scale, existingPoint.Y * scale); </i.e.> I can't seem to...
2
by: Microsoft | last post by:
Hi; I want to create a function point.With is point I can call the member function in a class. The Sample Code just like: __gc struct NodeMapItem { String *NodeID; void...
9
by: Grizlyk | last post by:
Grizlyk wrote: I need to point self class in template parameter, for example template < class Tptr, class Tc_iterator class Vcontainer { public:
12
by: EvilOldGit | last post by:
In Stroustrup he talks of a MathErr class that you can use to catch floating point exceptions. It doesn't exist, at least not in Visual C++ I can catch FP exceptions using catch(...) but am...
3
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I am a developer from C++ COM to C# COM. I feel confused about the entry point function for a C# COM object. In C++, we always initialize object through GetClassObject or...
0
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.