本地类实例无效模板参数 [英] Local class instances invalid template argument
问题描述
你好,
我认为使用仿函数的一个主要优点是排序
对函数的谓词就是我可以这样做:
void foo()
{
class Predicate
{
public:
bool operator()(...){...}
};
std :: list< whatever> lst;
// ...
std :: transform(lst.begin(),lst.end(),lst.begin(),Predicate()) ;
}
但是,我得到一个编译时错误type foo():: Predicate
由$?
为什么?
-
Matthias
Matthias写道:你好,
>我认为使用仿函数的一个主要优点是排序
对函数的谓词就是我可以做这样的事情:
void foo()
{
类Predicate
{
public:
bool operator()(...){...}
};
std :: list< whatever> lst;
// ...
std :: transform(lst.begin(),lst.end(),lst.begin(),Predicate());
}
不是一个有效的模板参数。
为什么?
糟糕,这应该是:
lst.sort(...,Predicate());
-
问候,
Matthias
Matthias写道:class Predicate
{
公开:
bool operator()(...){...} >};
确保它采用正确的参数。
-shez-
Shezan Baig写道:Matthias写道:
class Predicate
{
public :
bool operator()(...){...}
};
确保它采用正确的参数。
-shez -
当在功能范围之外使用完全相同的类时,一切都是
工作得很好。我怀疑它与论点有关。
-
问候,
Matthias
Hello,
I thought one major advantage of using functors as e.g. sorting
predicates over functions would be that I can do something like this:
void foo()
{
class Predicate
{
public:
bool operator() (...) {...}
};
std::list<whatever> lst;
// ...
std::transform( lst.begin(), lst.end(), lst.begin(), Predicate() );
}
However, I am getting a compile time error "type foo()::Predicate
composed from a local class is not a valid template-argument".
Why?
--
Regards,
Matthias
Matthias wrote:Hello,
I thought one major advantage of using functors as e.g. sorting
predicates over functions would be that I can do something like this:
void foo()
{
class Predicate
{
public:
bool operator() (...) {...}
};
std::list<whatever> lst;
// ...
std::transform( lst.begin(), lst.end(), lst.begin(), Predicate() );
}
However, I am getting a compile time error "type foo()::Predicate
composed from a local class is not a valid template-argument".
Why?
Whoops, this should read:
lst.sort( ..., Predicate() );
--
Regards,
Matthias
Matthias wrote:class Predicate
{
public:
bool operator() (...) {...}
};
Make sure it takes the correct arguments.
-shez-
Shezan Baig wrote:Matthias wrote:class Predicate
{
public:
bool operator() (...) {...}
};
Make sure it takes the correct arguments.
-shez-
When taking the exact same class outside the function scope, everything
works just fine. I doubt it had something to do with the arguments.
--
Regards,
Matthias
这篇关于本地类实例无效模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!