avatar
[菜鸟问题]类模板问题# Programming - 葵花宝典
d*e
1
template
class MyClass
{
public:
class iterator
{
iterator operator ++ (int);
};
private:
....
};
// -- implementation
template
// -- next line got error message
MyClass::iterator MyClass::iterator::operator ++ (int)
{
iterator iter(*this);
........
return iter;
}
这个实现部分的函数头哪里有问题?我得到的g++错误信息:
error: expected constructor, destructor, or type conversion before "MyClass"
error: expected `;' before "MyClass"
avatar
o*r
2
嵌套类?

【在 d******e 的大作中提到】
: template
: class MyClass
: {
: public:
: class iterator
: {
: iterator operator ++ (int);
: };
: private:
: ....

avatar
d*e
3
是。

【在 o******r 的大作中提到】
: 嵌套类?
avatar
t*t
4
missed ";"?

【在 d******e 的大作中提到】
: template
: class MyClass
: {
: public:
: class iterator
: {
: iterator operator ++ (int);
: };
: private:
: ....

avatar
d*e
5
a more clear version as following:
template
class MyClass
{
public:
class Inner
{
public:
Inner(int v = 0): value(v) {};
private:
int value;
};

Inner CreateInner(int v = 0) const;
private:
T TValue;

};
template
MyClass::Inner MyClass::CreateInner(int v) const // error
{
return Inner(v);
}
It seems the problem is in return type, because it has no problem if I
implement it as inline:
Inner CreateInner(int v = 0) const
avatar
t*t
6
typename MyClass::Inner MyClass::CreateInner(int v) const
~~~~~~~~
{...}

【在 d******e 的大作中提到】
: a more clear version as following:
: template
: class MyClass
: {
: public:
: class Inner
: {
: public:
: Inner(int v = 0): value(v) {};
: private:

avatar
d*e
7
I don't understand....
MyClass::Inner is supposed to be the return type. what does 'typename'
mean?
Thanks for response anyway.

【在 t****t 的大作中提到】
: typename MyClass::Inner MyClass::CreateInner(int v) const
: ~~~~~~~~
: {...}

avatar
t*t
8
since this is a template, myclass::inner is not necessary a type -- or at
least compiler doesn't know that (to be precise, it's a dependent name). so
you have to tell the compiler, it's a type.

【在 d******e 的大作中提到】
: I don't understand....
: MyClass::Inner is supposed to be the return type. what does 'typename'
: mean?
: Thanks for response anyway.

avatar
d*e
9
Yes, it worked, with either class or typename. but I still don't quite
understand. I already defined class MyClass 和 class Inner, what else could
MyClass::Inner could be except a class? with any T, can MyClass::Inner
mean anything else?

at
so

【在 t****t 的大作中提到】
: since this is a template, myclass::inner is not necessary a type -- or at
: least compiler doesn't know that (to be precise, it's a dependent name). so
: you have to tell the compiler, it's a type.

avatar
t*t
10
sure it can, have you heard of template specialization?

could

【在 d******e 的大作中提到】
: Yes, it worked, with either class or typename. but I still don't quite
: understand. I already defined class MyClass 和 class Inner, what else could
: MyClass::Inner could be except a class? with any T, can MyClass::Inner
: mean anything else?
:
: at
: so

avatar
d*e
11
i c. Thanks for the leading.

【在 t****t 的大作中提到】
: sure it can, have you heard of template specialization?
:
: could

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。