由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - [菜鸟问题]类模板问题
相关主题
c++ template question:请教一下这个template function在gcc下要怎么修改
C++ 菜鸟问一个关于template 的问题。template
a c++ questionSTL感觉实在太变态了
template questionstl 的 member type 看起来挺头大的
C++ question about template typedef讨论 找单链表倒数m的节点 (转载)
vector::iterator不对一个partial specialization的问题
请教 C++ std::list iterator 对 template class pointer 的应用问题iterator一问
template 疑问这两个地方是否需要typename?
相关话题的讨论汇总
话题: myclass话题: inner话题: class话题: titem话题: iterator
进入Programming版参与讨论
1 (共1页)
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

1 (共1页)
进入Programming版参与讨论
相关主题
这两个地方是否需要typename?C++ question about template typedef
这段 C++ 怎么改才能编译?vector::iterator不对
C++ template Questions (转载)请教 C++ std::list iterator 对 template class pointer 的应用问题
C++中使用back_inserter为啥可以不用#include 和using std::back_inserter;??template 疑问
c++ template question:请教一下这个template function在gcc下要怎么修改
C++ 菜鸟问一个关于template 的问题。template
a c++ questionSTL感觉实在太变态了
template questionstl 的 member type 看起来挺头大的
相关话题的讨论汇总
话题: myclass话题: inner话题: class话题: titem话题: iterator