明确声明 [英] Explicit declaration
问题描述
您好,
我正在尝试使用gcc将一些代码从Windows移植到Mac OSX。
假设我们有一个课程列表的工作方式如下:
模板< class T>
班级列表
{
受保护:
vector< T> m_List;
public:
typedef typename vector< T> :: iterator iterator;
iterator begin(){return m_List.begin() ;}
iterator end(){return m_List.end();}
}
现在我有一个派生类扩展了这一个:
模板< class T>
类ServerList
{
void Func();
}
模板< class T>
void ServerList< T> :: Func()
{
//这是有问题的部分
iterator it;
vector< iterator> ItVector;
....
}
}
Gcc不接受"迭代"而微软编译器呢。我老实说
不确定哪个是正确的,或者它是否是未定义的行为。在这种情况下,但是
我想要的是ServerList :: iterator(来自List)。
所以我尝试通过手动明确地声明它来修复它:
typename ServerList< T> :: iterator it;
vector< ServerList< T> :: iterator> ItVector;
这是正确的方法吗?
提前致谢。
- John >
John Smith写道:我正在尝试使用gcc将一些代码从Windows移植到Mac OSX。
假设我们有一个类似列表:
模板< class T>
班级列表
{
受保护:
向量< T> m_List;
public:
typedef typename vector< T> :: iterator iterator;
iterator begin(){return m_List.begin();}
iterator end(){return m_List.end();}
}
;
现在我有一个派生类扩展了这个:
模板< class T> <类ServerList
{
void Func();
}
;
你不是只是说你_derived_一类?不应该是
模板< class T> class ServerList:public List< T>
???
模板< class T>
void ServerList< T> :: Func()
{
//这是有问题的部分
iterator it;
vector< iterator> ItVector;
......
}
Gcc不接受迭代器而微软编译器呢。老实说,我不确定哪个是正确的,或者它是否是未定义的行为。在这种情况下,但我想要的是ServerList :: iterator(来自List)。
''iterator''是''ServerList< T>''中的从属名称(假设它*是*在
中,事实来自''列表与LT; T> '')。你必须明确地将它带入
''ServerList< T>'的范围或完全限定它。
所以我试图通过手动明确声明它来解决它像这样:
typename ServerList< T> :: iterator it;
vector< ServerList< T> :: iterator> ItVector;
这是正确的方法吗?
似乎很好。
附注:你真的想要一个像列表这样的用词不当吗?这实际上是
avector?我目前正在维护一个充满了这些误解的程序
并且它是一个PITA。
V
< blockquote> John Smith写道:
你好,我想用gcc将一些代码从Windows移植到Mac OSX。
假设我们有一个类列表,其工作方式如下:
模板< class T>
类列表
{
protected >>
vector< T> m_List;
public:
typedef typename vector< T> :: iterator iterator;
iterator begin(){return m_List.begin();}
iterator end(){return m_List.end();}
}
;需要分号
现在我有一个派生类扩展了这个:
模板< class T>
:公开列表< T>
//我怀疑你打算添加这个
类ServerList
{
void Func() ;
}
; //需要simicolon
模板< class T>
void ServerList< T> :: Func()
//
//这是有问题的部分
iterator it;
vector< iterator> ItVector;
......
}
Gcc不接受迭代器而微软编译器呢。老实说,我不确定哪个是正确的,或者它是否是未定义的行为。在这种情况下,但我想要的是ServerList :: iterator(来自List)。
所以我尝试通过手动明确地声明它来修复它:
typename ServerList< T> :: iterator it;
vector< ServerList< T> :: iterator> ItVector;
vector< typename ServerList< T> :: iterator> ItVector;
这是正确的方法吗?
是
有缺陷报告DR224涵盖了这一点,并且几天前由Scott Meyers在comp.std.c ++上发起了讨论。
>你不是只是说你来自一个班级吗?那不应该是
模板< class T> class ServerList:public List< T>
???
是的应该是。对不起......我刚刚写了一些简单的伪代码,所以我不需要复制真正的代码,这对于阅读来说有点复杂。
一面注意:你真的想要一个像列表这样的误称吗?那实际上是一个矢量?我目前正在维护一个充满了这些误解的程序
它是一个PITA。
不,我不想要,我已经使用了更好的名字。再次,这只是为了说明问题。
感谢您的回答。
- John
Hello,
I''m trying to port some code from Windows to Mac OSX with gcc.
Assume we have a class list which works like this:
template<class T>
class List
{
protected:
vector <T> m_List;
public:
typedef typename vector<T>::iterator iterator;
iterator begin() {return m_List.begin();}
iterator end() {return m_List.end();}
}
Now I have a derived class which extends this one:
template<class T>
class ServerList
{
void Func();
}
template<class T>
void ServerList<T>::Func()
{
// Here is the problematic part
iterator it;
vector<iterator> ItVector;
....
}
}
Gcc doesn''t accept "iterator" whereas microsoft compiler does. I''m honestly
not sure which is correct or if it''s "undefined behavior" in this case, but
what I want is the ServerList::iterator (from List).
So I tried to fix it by manually explicitly declaring it like this:
typename ServerList<T>::iterator it;
vector<ServerList<T>::iterator> ItVector;
Is this the correct way?
Thanks in advance.
-- John
John Smith wrote:I''m trying to port some code from Windows to Mac OSX with gcc.
Assume we have a class list which works like this:
template<class T>
class List
{
protected:
vector <T> m_List;
public:
typedef typename vector<T>::iterator iterator;
iterator begin() {return m_List.begin();}
iterator end() {return m_List.end();}
} ;
Now I have a derived class which extends this one:
template<class T>
class ServerList
{
void Func();
} ;
Didn''t you just say you _derived_ a class? Shouldn''t it then be
template<class T> class ServerList : public List<T>
???
template<class T>
void ServerList<T>::Func()
{
// Here is the problematic part
iterator it;
vector<iterator> ItVector;
...
}
}
Gcc doesn''t accept "iterator" whereas microsoft compiler does. I''m honestly
not sure which is correct or if it''s "undefined behavior" in this case, but
what I want is the ServerList::iterator (from List).
''iterator'' is a dependent name in ''ServerList<T>'' (provided it *is* in
fact derived from ''List<T>''). You have to explicitly bring it into the
''ServerList<T>''s scope or fully qualify it.
So I tried to fix it by manually explicitly declaring it like this:
typename ServerList<T>::iterator it;
vector<ServerList<T>::iterator> ItVector;
Is this the correct way?
Seems fine.
A side note: do you really want such a misnomer as "List" that is actually
a "vector"? I am currently maintaining a program full of such misnomers
and it is a PITA.
V
John Smith wrote:Hello,
I''m trying to port some code from Windows to Mac OSX with gcc.
Assume we have a class list which works like this:
template<class T>
class List
{
protected:
vector <T> m_List;
public:
typedef typename vector<T>::iterator iterator;
iterator begin() {return m_List.begin();}
iterator end() {return m_List.end();}
} ; need semicolon
Now I have a derived class which extends this one:
template<class T> : public List<T>
// I suspect you meant to add this
class ServerList
{
void Func();
} ; // need simicolon
template<class T>
void ServerList<T>::Func()
{
// Here is the problematic part
iterator it;
vector<iterator> ItVector;
...
}
}
Gcc doesn''t accept "iterator" whereas microsoft compiler does. I''m honestly
not sure which is correct or if it''s "undefined behavior" in this case, but
what I want is the ServerList::iterator (from List).
So I tried to fix it by manually explicitly declaring it like this:
typename ServerList<T>::iterator it;
vector<ServerList<T>::iterator> ItVector; vector<typename ServerList<T>::iterator> ItVector;
Is this the correct way?
yes
There is a defect report DR224 that covers this and there is a
discussion initiated by Scott Meyers on comp.std.c++ a few days ago.
> Didn''t you just say you _derived_ a class? Shouldn''t it then be
template<class T> class ServerList : public List<T>
???
Yes it should be. Sorry... I just wrote some simple pseudo code so I
wouldn''t have to copy the real code which is a tad more complex to read.
A side note: do you really want such a misnomer as "List" that is actually
a "vector"? I am currently maintaining a program full of such misnomers
and it is a PITA.
No I don''t want and I use better names already. Again this was just to
illustrate the problem.
Thanks for your answer.
-- John
这篇关于明确声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!