d******e 发帖数: 194 | 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" |
o******r 发帖数: 259 | 2 嵌套类?
【在 d******e 的大作中提到】 : template : class MyClass : { : public: : class iterator : { : iterator operator ++ (int); : }; : private: : ....
|
d******e 发帖数: 194 | 3 是。
【在 o******r 的大作中提到】 : 嵌套类?
|
t****t 发帖数: 6806 | 4 missed ";"?
【在 d******e 的大作中提到】 : template : class MyClass : { : public: : class iterator : { : iterator operator ++ (int); : }; : private: : ....
|
d******e 发帖数: 194 | 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 |
t****t 发帖数: 6806 | 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:
|
d******e 发帖数: 194 | 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 : ~~~~~~~~ : {...}
|
t****t 发帖数: 6806 | 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.
|
d******e 发帖数: 194 | 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.
|
t****t 发帖数: 6806 | 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
|
d******e 发帖数: 194 | 11 i c. Thanks for the leading.
【在 t****t 的大作中提到】 : sure it can, have you heard of template specialization? : : could
|