问题描述
我想用STL排序从std :: vector派生的类:
template< typename T,typename fitParaType,typename fitResType>
class Manipulator {
//我现在不能使用
来访问私人会员//比较?
朋友班比较;
public:
...
void sort(人口< Genome< T,fitParaType>>& pop);
private:
...
const Functor< fitParaType,fitResType> * _func;
class比较{
public:
比较(){}
bool operator()(基因组< T,fitParaType>& arg1,
Genome< T,fitParaType>& arg2)
{
return(_func-> value(arg1.getParams())
< _func-> value(arg2.getParams()));
}
};
};
模板< typename T,typename fitParaType,typename fitResType>
void操纵器< T,适合ParaType,fitResType> :: sort(Population< Genome< T,
fitParaType> >&安培;流行)
{
//人口来自std :: vector
std :: sort(pop.begin(),pop。 end(),Manipulator< T,fitParaType,
fitResType> :: Compare());
}
//实例化,所以它编译
模板类操纵器< RealGene< double>,double,double> ;;
我正在尝试比较朋友类的操纵器,所以我可以使用
_func函数。但是,我得到:
/Manipulator.cpp:35:从这里实例化
... / Manipulator.hpp:64:错误:''类操纵器< RealGene< ; double>,double,
double> :: Compare''没有名为''_func'的成员'
如何编写一个Compare类,我可以用来排序吗?有没有比我一直尝试的更好的方式?
谢谢,
Paul Schneider
这是一个棘手的领域,我认为最近标准已经改变了关于内部阶级和友谊的标准。但是以下可能更有可能工作
模板< typename T,typename fitParaType,typename fitResType>
类操纵器{
类比较;
朋友类比较;
如果没有前向声明,编译器认为朋友类
比较是指在Manipulator之外声明的类,而不是您稍后声明的内部
类。
john
-
问候,
巴斯特。
[snip]
这个是一个棘手的领域,我认为最近有关内部阶级和友谊的标准已经改变了。但是以下可能更有可能工作
模板< typename T,typename fitParaType,typename fitResType>
类操纵器{
类比较;
朋友类比较;
没有前向声明编译器认为朋友类
比较是指在Manipulator之外声明的类,而不是您稍后声明的内部类。 />
john
谢谢,在尝试了你的建议之后,朋友的声明仍然没有工作。 (指针被排除在外吗?)。现在我尝试了以下比较课程:
class比较{
public:
比较(const Functor< fitParaType,fitResType> * func):_ func(func){}
bool operator()(Genome< T,fitParaType>& arg1,
Genome< T,fitParaType>& arg2 )
{
return(_func-> value(arg1.getParams())
< _func-> value(arg2。 getParams()));
}
private:
const Functor< fitParaType,fitResType> * _func;
$ b $实现sort函数的
:
模板< typename T,typename fitParaType,typename fitResType>
void Manipulator< T,fitParaType,fitResType> :: sort(Population< Genome< T,
fitParaType>>& pop)
{
std :: sort(pop.begin(),pop.end(),this-> Compare(_func));
}
这只给我一个错误:
... / Manipulator.cpp:32:错误:调用非函数`类
操纵器< RealGene< double>,double,double> ::比较''
如何摆脱这个?
再次感谢,
Paul
I want to sort a class derived from std::vector with STL sort.:
template<typename T, typename fitParaType, typename fitResType>
class Manipulator{
// shouldn''t I now be able to access private members with
// Compare?
friend class Compare;
public:
...
void sort(Population<Genome<T, fitParaType> >& pop);
private:
...
const Functor<fitParaType, fitResType>* _func;
class Compare{
public:
Compare(){}
bool operator () (Genome<T, fitParaType>& arg1,
Genome<T, fitParaType>& arg2)
{
return (_func->value(arg1.getParams())
< _func->value(arg2.getParams()));
}
};
};
template<typename T, typename fitParaType, typename fitResType>
void Manipulator<T, fitParaType, fitResType>::sort(Population<Genome<T,
fitParaType> >& pop)
{
// Population is derived from std::vector
std::sort(pop.begin(), pop.end(), Manipulator<T, fitParaType,
fitResType>::Compare() );
}
// instantiation, so it compiles
template class Manipulator<RealGene<double>, double, double>;
I am trying to make Compare a friend class of Manipulator, so I can use
the _func function. However, I am getting:
/Manipulator.cpp:35: instantiated from here
.../Manipulator.hpp:64: error: ''class Manipulator<RealGene<double>, double,
double>::Compare'' has no member named ''_func''
How can I write a Compare class that I can use for sorting? Is there a
better way than the way I have been trying?
Thanks,
Paul Schneider
This is a tricky area, and I think the standard has changed recently
concerning inner classes and friendship. However the following is probably
more likely to work
template<typename T, typename fitParaType, typename fitResType>
class Manipulator{
class Compare;
friend class Compare;
Without the forward declaration the compiler thinks that friend class
Compare refers to a class declared outside of Manipulator, not the inner
class you declare later.
john
--
Regards,
Buster.
[snip]
This is a tricky area, and I think the standard has changed recently
concerning inner classes and friendship. However the following is probably
more likely to work
template<typename T, typename fitParaType, typename fitResType>
class Manipulator{
class Compare;
friend class Compare;
Without the forward declaration the compiler thinks that friend class
Compare refers to a class declared outside of Manipulator, not the inner
class you declare later.
john
Thanks, after trying your suggestion the friend statement still doesn''t
work. (Are pointers excluded?). Now I tried the following Compare class:
class Compare{
public:
Compare(const Functor<fitParaType, fitResType>* func) : _func(func){}
bool operator () (Genome<T, fitParaType>& arg1,
Genome<T, fitParaType>& arg2)
{
return (_func->value(arg1.getParams())
< _func->value(arg2.getParams()));
}
private:
const Functor<fitParaType, fitResType>* _func;
};
with implementation of the sort function:
template<typename T, typename fitParaType, typename fitResType>
void Manipulator<T, fitParaType, fitResType>::sort(Population<Genome<T,
fitParaType> >& pop)
{
std::sort(pop.begin(), pop.end(), this->Compare(_func) );
}
This gives me only one error:
.../Manipulator.cpp:32: error: call to non-function `class
Manipulator<RealGene<double>, double, double>::Compare''
How can I get rid of this?
Thanks again,
Paul
这篇关于STL排序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!