473,479 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

simple template question

Hi everyone,

I'm trying to create two simple template classes where one of them
depends on the other to initialize its member integer variable. After
some time, I decided to post a message here because I don't really
understand why I can't make this work.

template<int n>
class ClassA
{
public:
int n_;

ClassA() : n_(n)
{}
};

template<class ClassA>
class ClassB
{
public:
int m_;

ClassB() : m_(ClassA::n_)
{}
};

int main()
{
typedef ClassA<2ca;
typedef ClassB<cacb;

cb cb1;
return 1;
}

The compiler errors that I get:

aaragon@aaragon-laptop:~/Desktop$ g++ test.cxx
test.cxx: In constructor 'ClassB<ClassA>::ClassB() [with ClassA =
ClassA<2>]':
test.cxx:28: instantiated from here
test.cxx:5: error: object missing in reference to 'ClassA<2>::n_'
test.cxx:17: error: from this location

I guess that the error lies on the fact that a typedef is not an
instantiation of the object so therefore I cannot call the variable n
within ClassA. On the other hand, I cannot instantiate ClassA to
create the type definition of classB because I receive a compiler
error that tells me that the instantiated object of ClassA cannot
appear in a constant expression.

Is there a way around this? Maybe the solution is real simple. Thank
you.

May 26 '07 #1
11 1483
aaragon wrote:
Hi everyone,

I'm trying to create two simple template classes where one of them
depends on the other to initialize its member integer variable. After
some time, I decided to post a message here because I don't really
understand why I can't make this work.

template<int n>
class ClassA
{
public:
int n_;

ClassA() : n_(n)
{}
};

template<class ClassA>
class ClassB
{
public:
int m_;

ClassB() : m_(ClassA::n_)
{}
};

int main()
{
typedef ClassA<2ca;
typedef ClassB<cacb;

cb cb1;
return 1;
}

The compiler errors that I get:

aaragon@aaragon-laptop:~/Desktop$ g++ test.cxx
test.cxx: In constructor 'ClassB<ClassA>::ClassB() [with ClassA =
ClassA<2>]':
test.cxx:28: instantiated from here
test.cxx:5: error: object missing in reference to 'ClassA<2>::n_'
test.cxx:17: error: from this location

I guess that the error lies on the fact that a typedef is not an
instantiation of the object so therefore I cannot call the variable n
within ClassA.
Right. You have a type, not an object, so there is no n_.
On the other hand, I cannot instantiate ClassA to create the type
definition of classB because I receive a compiler error that tells me that
the instantiated object of ClassA cannot appear in a constant expression.

Is there a way around this? Maybe the solution is real simple. Thank
you.
It depends on what you want to do. Maybe making n_ static is what you want.
May 26 '07 #2
aaragon wrote:
Hi everyone,

I'm trying to create two simple template classes where one of them
depends on the other to initialize its member integer variable. After
some time, I decided to post a message here because I don't really
understand why I can't make this work.

template<int n>
class ClassA
{
public:
int n_;
static const int n_ = n;

// the original definition is a member variable. What you want is a
"static". It also likely needs to be const. You can use an anonymous enum.
>
ClassA() : n_(n)
{}
Nuke the constructor.
....
May 26 '07 #3
On May 26, 3:25 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
aaragon wrote:
Hi everyone,
I'm trying to create two simple template classes where one of them
depends on the other to initialize its member integer variable. After
some time, I decided to post a message here because I don't really
understand why I can't make this work.
template<int n>
class ClassA
{
public:
int n_;

static const int n_ = n;

// the original definition is a member variable. What you want is a
"static". It also likely needs to be const. You can use an anonymous enum.
ClassA() : n_(n)
{}

Nuke the constructor.
...
That worked!!!
However, I don't understand why, could you briefly explain me why is
working?

May 26 '07 #4
aaragon wrote:
On May 26, 3:25 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
>aaragon wrote:
>>Hi everyone,
I'm trying to create two simple template classes where one of them
depends on the other to initialize its member integer variable. After
some time, I decided to post a message here because I don't really
understand why I can't make this work.
template<int n>
class ClassA
{
public:
int n_;
static const int n_ = n;

// the original definition is a member variable. What you want is a
"static". It also likely needs to be const. You can use an anonymous enum.
>> ClassA() : n_(n)
{}
Nuke the constructor.
...

That worked!!!
However, I don't understand why, could you briefly explain me why is
working?
Learn about "static" - it's one of the most overused keywords int the
language and means different things at different contexts.

-------------------

static const int A = 1; // use 1

struct T
{
static const int A = 1; // use 2

void f( int c )
{
static const int A = c; // use 3
}
};
--------------------
use 1 : this use of static is "deprecated". Anonymous namespaces is the
moral equivalent. In this use of static, A is only visible to the
compilation unit.

use 2 : This use indicates that there is only 1 instance of member A for
all classes and is initialized in global scope. When an integer type
POD is declared const above it may be used in constant expresssions
(declare the size of an array or use in template argument e.g.) non POD
static members must be initialized out of the class definition.

use 3 : static in a function block indicates there is only one A and
that it is initialized the first time control passes through the function.

Your fav. C++ reference book will have this fully described with
examples if it's worth anything.
May 26 '07 #5
Gianni Mariani wrote:
aaragon wrote:
>On May 26, 3:25 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
>>aaragon wrote:
Hi everyone,
I'm trying to create two simple template classes where one of them
depends on the other to initialize its member integer variable. After
some time, I decided to post a message here because I don't really
understand why I can't make this work.
template<int n>
class ClassA
{
public:
int n_;
static const int n_ = n;

// the original definition is a member variable. What you want is a
"static". It also likely needs to be const. You can use an
anonymous enum.

ClassA() : n_(n)
{}
Nuke the constructor.
...

That worked!!!
However, I don't understand why, could you briefly explain me why is
working?

Learn about "static" - it's one of the most overused keywords int the
language and means different things at different contexts.

-------------------

static const int A = 1; // use 1

struct T
{
static const int A = 1; // use 2

void f( int c )
{
static const int A = c; // use 3
}
};
--------------------
use 1 : this use of static is "deprecated". Anonymous namespaces is the
moral equivalent. In this use of static, A is only visible to the
compilation unit.

use 2 : This use indicates that there is only 1 instance of member A for
all classes and is initialized in global scope. When an integer type
..... I meant to write :
use 2 : This use indicates that there is only 1 instance of member A for
all OBJECTS of this class ...

POD is declared const above it may be used in constant expresssions
(declare the size of an array or use in template argument e.g.) non POD
static members must be initialized out of the class definition.

use 3 : static in a function block indicates there is only one A and
that it is initialized the first time control passes through the function.

Your fav. C++ reference book will have this fully described with
examples if it's worth anything.
May 26 '07 #6
On May 26, 4:07 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
aaragon wrote:
On May 26, 3:25 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
aaragon wrote:
Hi everyone,
I'm trying to create two simple template classes where one of them
depends on the other to initialize its member integer variable. After
some time, I decided to post a message here because I don't really
understand why I can't make this work.
template<int n>
class ClassA
{
public:
int n_;
static const int n_ = n;
// the original definition is a member variable. What you want is a
"static". It also likely needs to be const. You can use an anonymous enum.
> ClassA() : n_(n)
{}
Nuke the constructor.
...
That worked!!!
However, I don't understand why, could you briefly explain me why is
working?

Learn about "static" - it's one of the most overused keywords int the
language and means different things at different contexts.

-------------------

static const int A = 1; // use 1

struct T
{
static const int A = 1; // use 2

void f( int c )
{
static const int A = c; // use 3
}};

--------------------
use 1 : this use of static is "deprecated". Anonymous namespaces is the
moral equivalent. In this use of static, A is only visible to the
compilation unit.

use 2 : This use indicates that there is only 1 instance of member A for
all classes and is initialized in global scope. When an integer type
POD is declared const above it may be used in constant expresssions
(declare the size of an array or use in template argument e.g.) non POD
static members must be initialized out of the class definition.

use 3 : static in a function block indicates there is only one A and
that it is initialized the first time control passes through the function.

Your fav. C++ reference book will have this fully described with
examples if it's worth anything.
Thank you...

May 26 '07 #7
aaragon wrote:
On May 26, 3:25 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
>aaragon wrote:
>>Hi everyone,
I'm trying to create two simple template classes where one of them
depends on the other to initialize its member integer variable. After
some time, I decided to post a message here because I don't really
understand why I can't make this work.
template<int n>
class ClassA
{
public:
int n_;
static const int n_ = n;

// the original definition is a member variable. What you want is a
"static". It also likely needs to be const. You can use an anonymous enum.
>> ClassA() : n_(n)
{}
Nuke the constructor.
...

That worked!!!
However, I don't understand why, could you briefly explain me why is
working?
CORRECTED VERSION - cancelling previous version...

Learn about "static" - it's one of the most overused keywords int the
language and means different things at different contexts.

-------------------

static const int A = 1; // use 1

struct T
{
static const int A = 1; // use 2

void f( int c )
{
static const int A = c; // use 3
}
};
--------------------
use 1 : this use of static is "deprecated". Anonymous namespaces is the
moral equivalent. In this use of static, A is only visible to the
compilation unit.

use 2 : This use indicates that there is only 1 instance of member A for
all objects of this class and is initialized in global scope which makes
A in this use globally accessible. When an integer type POD is declared
const like above it may be used in constant expresssions (declare the
size of an array or use in template argument e.g.) non POD static
members must be initialized out of the class definition.

use 3 : static in a function block indicates there is only one A and
that it is initialized the first time control passes through the function.

Your fav. C++ reference book will have this fully described with
examples if it's worth anything.

May 26 '07 #8
On May 27, 7:17 am, Gianni Mariani <gi3nos...@mariani.wswrote:
aaragon wrote:
On May 26, 3:25 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
aaragon wrote:
....
>
CORRECTED VERSION - cancelling previous version...
....

It appears that after all these years, there are still bugs in
cancelling messages ! I don't think I have ever seen it work. We'll
try again in another 5 years.

May 26 '07 #9
On May 26, 11:32 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
On May 27, 7:17 am, Gianni Mariani <gi3nos...@mariani.wswrote:aaragonwrote:
On May 26, 3:25 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
>aaragon wrote:
...
CORRECTED VERSION - cancelling previous version...
...
It appears that after all these years, there are still bugs in
cancelling messages ! I don't think I have ever seen it work. We'll
try again in another 5 years.
Because of the large number of forged cancels, I believe that
most sites today simply ignore them. (They worked, for me,
fifteen years ago. But not now.)

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
May 27 '07 #10
James Kanze wrote:
On May 26, 11:32 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
>On May 27, 7:17 am, Gianni Mariani <gi3nos...@mariani.wswrote:aaragon wrote:
>>>On May 26, 3:25 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
aaragon wrote:
...
>>CORRECTED VERSION - cancelling previous version...
>...
>It appears that after all these years, there are still bugs in
cancelling messages ! I don't think I have ever seen it work. We'll
try again in another 5 years.

Because of the large number of forged cancels, I believe that
most sites today simply ignore them. (They worked, for me,
fifteen years ago. But not now.)
I don't think it ever worked for me - can't they use a PKI type auth ?
May 27 '07 #11
On May 27, 10:25 am, Gianni Mariani <gi3nos...@mariani.wswrote:
James Kanze wrote:
[...]
It appears that after all these years, there are still bugs in
cancelling messages ! I don't think I have ever seen it work. We'll
try again in another 5 years.
Because of the large number of forged cancels, I believe that
most sites today simply ignore them. (They worked, for me,
fifteen years ago. But not now.)
I don't think it ever worked for me - can't they use a PKI type auth ?
[This is getting awfully off topic...]

In theory, there are any number of solutions which could be
used. In practice, the mass of systems out there is now too
large to start playing with the protocol; add any formal
authentication, and it will be years before all, or even most,
of the systems will support it.

Anyway, I did successfully cancel some messages I'd posted back
in the early 1990's.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
May 27 '07 #12

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

Similar topics

8
2573
by: LaBird | last post by:
Hi, I have a simple question about templates. When I have a class definition like this in "class.h": template <typename T> class C { T p; public:
1
1761
by: Scott | last post by:
The following is the XML I have to work with. Below is the question <Table0> <CaseID>102114</CaseID> <CaseNumber>1</CaseNumber> <DateOpened>2005-06-14T07:26:00.0000000-05:00</DateOpened>...
3
1496
by: Winston Smith | last post by:
Hi, I have question about compiling multiple source files with templates using gcc. It's probably obvious but I'm new to C++. I've tried to isolate my problem and it came down to this. Lets...
13
1219
by: dgront | last post by:
Maybe my question is too simple, but I've spent some time on it and still don't know.. I need a template function, converting from string to other data types: template <typename T>...
8
5088
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
0
1868
by: 42 | last post by:
I implemented a simple class inherited from Page to create a page template. It simply wraps some trivial html around the inherited page, and puts the inherited page into a form. The problem I...
3
2303
by: Chrism2671 | last post by:
I'm new to XSLT/XML and I have a very simple, quick question. i've been trying to convert simple xml files into CSV files and have made a simple XSLT template using the w3 tutorials, but it doesn't...
3
1835
by: stdlib99 | last post by:
Hi, I have a simple question regarding templates and meta programming. I am going to try and work my way through the C++ Template Metaprogramming, a book by David Abrahams and Aleksey...
17
5783
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /*...
0
7033
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
7071
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...
1
6726
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...
0
6861
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...
0
4468
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2987
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...
0
2974
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
170
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.