By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,904 Members | 1,302 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,904 IT Pros & Developers. It's quick & easy.

simple template question

P: n/a
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
Share this Question
Share on Google+
11 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.