简单的模板问题 [英] simple template question

查看:87
本文介绍了简单的模板问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在尝试创建两个简单的模板类,其中一个

依赖于另一个来初始化其成员整数变量。

一段时间之后,我决定在这里发布一条消息,因为我不是真的

明白为什么我不能做这个工作。


模板< int n>

class ClassA

{

public:

int n_;


ClassA():n_(n)

{}

};


模板< class ClassA>

class ClassB

{

public:

int m _;


ClassB():m_(ClassA :: n_)

{}

};


int main()

{

typedef ClassA< 2ca;

typedef ClassB< cacb;


cb cb1;

返回1;

}


我得到的编译器错误:


aaragon @ aaragon-laptop:〜/ Desktop $ g ++ test.cxx

test.cxx:在构造函数''ClassB< ClassA> :: ClassB()[with ClassA =

ClassA< 2>]'':

test.cxx:28:从这里实例化

test.cxx:5:错误或:参考''ClassA< 2> :: n_'''缺少对象
test.cxx:17:错误:从这个位置


我猜测错误在于typedef不是对象的实例化,因此我不能在ClassA中调用变量n

。另一方面,我无法将ClassA实例化为

创建classB的类型定义,因为我收到编译器

错误,告诉我ClassA的实例化对象不能

出现在一个常量表达式中。


有没有办法解决这个问题?也许解决方案非常简单。谢谢

你。

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.

推荐答案

g ++ test.cxx

test。 cxx:在构造函数''ClassB< ClassA> :: ClassB()[with ClassA =

ClassA< 2>]'':

test.cxx:28:instantiated从这里

test.cxx:5:错误:参考''ClassA< 2> :: n_''缺少对象

test.cxx:17:错误:从这个位置


我猜错误在于typedef不是对象的实例化,因此我无法调用变量n
ClassA中的
。另一方面,我无法将ClassA实例化为

创建classB的类型定义,因为我收到编译器

错误,告诉我ClassA的实例化对象不能

出现在一个常量表达式中。


有没有办法解决这个问题?也许解决方案非常简单。感谢

你。

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.


aaragon写道:
aaragon wrote:

嗨每个人,


我正在尝试创建两个简单的模板类,其中一个

依赖于另一个来初始化其成员整数变量。

一段时间之后,我决定在这里发布一条消息,因为我不是真的

明白为什么我不能做这个工作。


模板< int n>

class ClassA

{

public:

int n_;


ClassA():n_(n)

{}

};


模板< class ClassA>

class ClassB

{

public:

int m _;


ClassB():m_(ClassA :: n_)

{}

};


int main()

{

typedef ClassA< 2ca;

typedef ClassB< cacb;


cb cb1;

返回1;

}


我得到的编译器错误:


aaragon @ aaragon-laptop:〜/ Desktop
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:在构造函数中''ClassB< ; ClassA> :: ClassB()[with ClassA =

ClassA< 2>]'':

test.cxx:28:从这里实例化

test.cxx:5:错误:参考''ClassA< 2> :: n_''缺少对象

test.cxx:17:错误:来自此位置

我猜错误在于typedef不是对象的实例化,因此我无法调用变量n
$ b ClassA中的$ b。
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.



对。你有一个类型,而不是一个对象,所以没有n_。

Right. You have a type, not an object, so there is no n_.


另一方面,我无法实例化ClassA来创建类型

定义classB,因为我收到编译器错误,告诉我

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.



这取决于你想做什么。也许制作n_ static就是你想要的。

It depends on what you want to do. Maybe making n_ static is what you want.


这篇关于简单的模板问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆