473,668 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(i nt 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<nDi m,1& = AssertEqual<nDi m,1>() )
{
m_coordinate[0] = v0;
}

Point( double v0, double v1, AssertEqual<nDi m,2& =
AssertEqual<nDi m,2>() )
{
m_coordinate[0] = v0;
m_coordinate[1] = v1;
}

Point( double v0, double v1, double v2, AssertEqual<nDi m,3& =
AssertEqual<nDi m,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 3740
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(i nt 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<nDi m,1& = AssertEqual<nDi m,1>() )
{
m_coordinate[0] = v0;
}

Point( double v0, double v1, AssertEqual<nDi m,2& =
AssertEqual<nDi m,2>() )
{
m_coordinate[0] = v0;
m_coordinate[1] = v1;
}

Point( double v0, double v1, double v2, AssertEqual<nDi m,3& =
AssertEqual<nDi m,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<nDi m,N>:
template<int nDim>
class Point
{
public:
double GetCoordinate(i nt iDim)
{
return m_coordinate[iDim];
}
typedef AssertEqual<nDi m,1AssertEqual1 ;
typedef AssertEqual<nDi m,2AssertEqual2 ;
typedef AssertEqual<nDi m,3AssertEqual3 ;
// define all the constuctors adding a default last parameter
// whose type will have an error if it is instantiated
Point( double v0, AssertEqual<nDi m,1& = AssertEqual<nDi m,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<nDi m,N>:
template<int nDim>
class Point
{
public:
double GetCoordinate(i nt iDim)
{
return m_coordinate[iDim];
}

typedef AssertEqual<nDi m,1AssertEqual1 ;
typedef AssertEqual<nDi m,2AssertEqual2 ;
typedef AssertEqual<nDi m,3AssertEqual3 ;
// define all the constuctors adding a default last parameter
// whose type will have an error if it is instantiated
Point( double v0, AssertEqual<nDi m,1& = AssertEqual<nDi m,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,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 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,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++.

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,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++.
Hi everyone, I was able to compile the code but I did something
somewhat different. This is what I do:
template<boolst ruct ConstAssert;
template<struct ConstAssert<tru e{};

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<tru e>())
{
coord[0] = x;
coord[1] = y;
}

// 3D point constructor
Point(T x, T y, T z, ConstAssert<n == 3= ConstAssert<tru e>())
{
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<fa lse>' has type 'ConstAssert<tr ue>'
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<fal se>'

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.comwro te:
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<nDi m,1& = AssertEqual<nDi m,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<nDi m, 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
7842
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 long int to store the value, and the precision (number of decimal points) is variable (it's a templated class): template <size_t _decimal_places = 4> class FixedFloat {
1
16230
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 to the fact1 is given as since the derived object always consists of the base part , the base class pointer will always point to the base part in the derived object unless otherwise the function in the base class are declared as virtual and are...
10
3317
by: Bhan | last post by:
Using Ptr of derived class to point to base class and viceversa class base { .... } class derived : public base { .... }
3
2759
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 create my own class and inherit from Point, since it is
2
1339
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 (*FuncPnt)(reportNode *); };
9
1620
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
1787
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 stumped in finding out what the class I'm catching is. I would like to know how to catch them in a more elegant way than catch(...) and then poling around in registers to guess the problem. TIA
3
5647
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 DLLGetClassObject, but it seems C# does not need these functions? Here is a sample from MSDN.
0
2186
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 problems later). Apparently Point is a struct, a value type, and it does not behave like a classic structure (in my mind's eye, and see below). Traditionally I think of a classic structure as simply an object where every member is public. But with...
0
8459
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8890
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8791
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8577
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8653
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7398
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4376
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2018
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.