自定义比较器和模板的具体问题
很抱歉问了这么多,但我遇到了另一个问题,我不知道如何解决...据我所知,gcc无法解决myComparator class类型,可能是因为下面的代码不符合标准。问题是我是否遗漏了某些内容,或者是否有解决此问题的方法,这不需要太多更改(例如接口解决方案)...
template <typename DATA> class myArray
{
template <typename F> void sort (F &comp)
{
// No problems here
}
template <typename T> void sort(void)
{
T::myComparator cmp; //Error: expected `;' before 'zzz'
// T::template myComparator cmp; also doesn't work
sort(cmp);
}
};
class test
{
public:
class myComparator
{
public:
bool operator() ( const test *t1, const test * t2)
{
// No problems here
}
};
};
void testCmp()
{
myComparator cmp;
cmp.sort<test>();
}
I'm sorry for asking so much, but I've encountered another problem I have no idea how to solve... From what I gather, gcc fails to resolve myComparator class type, probably because the following code is not compliant with the standard. The question is if I am missing something or is there a workaround for this problem, which wouldn't require too much changes (like interfaces solution)...
template <typename DATA> class myArray
{
template <typename F> void sort (F &comp)
{
// No problems here
}
template <typename T> void sort(void)
{
T::myComparator cmp; //Error: expected `;' before 'zzz'
// T::template myComparator cmp; also doesn't work
sort(cmp);
}
};
class test
{
public:
class myComparator
{
public:
bool operator() ( const test *t1, const test * t2)
{
// No problems here
}
};
};
void testCmp()
{
myComparator cmp;
cmp.sort<test>();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须使用
typename
作为:typename
是必需的,因为myComparator
是一个从属名称。请参阅 stackoverflow 本身的 C++ 常见问题解答:
“我必须在何处以及为何将
template
和typename
放在依赖名称上?”You've to use
typename
as:typename
is required becausemyComparator
is a dependent name.See this C++ FAQ at stackoverflow itself:
"Where and why do I have to put
template
andtypename
on dependent names?"