468,727 Members | 1,659 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,727 developers. It's quick & easy.

STL sort question

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

Jul 22 '05 #1
10 1566

"Paul Schneider" <pa*******@uboot.com> wrote in message
news:c6*********@bird.wu-wien.ac.at...
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:
... [snip]
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
Jul 22 '05 #2
Paul Schneider wrote:
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;
I'm not sure, but I think that should be "friend class
Manipulator::Compare;", so as not to forward-declare a class at
namespace scope. That's not your biggest problem, though.
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()));
A Manipulator::Compare object is quite different from a Manipulator
object. You can only access the non-static member _func of Manipulator
through a Manipulator object.

Perhaps the Compare object should contain its own copy of _func, or a
reference to the Manipulator object which created the Compare object.

I don't know what _func does. Are you sure it defines a strict weak
ordering, as required by std::sort? A common way to compare vectors
is lexicographically -- so common, in fact, that you can compare two
std::vectors with < or std::less.
}
};
};

template<typename T, typename fitParaType, typename fitResType>
void Manipulator<T, fitParaType, fitResType>::sort(Population<Genome<T,
fitParaType> >& pop)
{
// Population is derived from std::vector
You should probably using delegation rather than inheritance, but it's
your call.
std::sort(pop.begin(), pop.end(), Manipulator<T, fitParaType,
fitResType>::Compare() );
}
// instantiation, so it compiles
template class Manipulator<RealGene<double>, double, double>;
Are you sure that's necessary? You should probably not be compiling the
template definitions separately. There is an entry about this in the
FAQ.
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?

--
Regards,
Buster.
Jul 22 '05 #3
John Harrison wrote:
"Paul Schneider" <pa*******@uboot.com> wrote in message
news:c6*********@bird.wu-wien.ac.at...
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:
...


[snip]
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

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

Jul 22 '05 #4
Paul Schneider wrote:
John Harrison wrote:
"Paul Schneider" <pa*******@uboot.com> wrote in message
news:c6*********@bird.wu-wien.ac.at...
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:
...

[snip]
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

Thanks, after trying your suggestion the friend statement still doesn't
work. (Are pointers excluded?).


No, pointers aren't excluded. Friendship is granted on a per-class
basis. Compare gets access to any Manipulator object, no matter how it's
referenced.

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'
At least give us a fighting chance. Where's line 32?
How can I get rid of this?


--
Regards,
Buster.
Jul 22 '05 #5
Paul Schneider <pa*******@uboot.com> wrote in
news:c6*********@bird.wu-wien.ac.at:
John Harrison wrote:
"Paul Schneider" <pa*******@uboot.com> wrote in message
news:c6*********@bird.wu-wien.ac.at...
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:
...


[snip]
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

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?


std::sort(pop.begin(), pop.end(), Compare(_func));
Jul 22 '05 #6
Buster wrote:

No, pointers aren't excluded. Friendship is granted on a per-class
basis. Compare gets access to any Manipulator object, no matter how it's
referenced.

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'

At least give us a fighting chance. Where's line 32?
How can I get rid of this?


line 32 is:
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) );
}

P

Jul 22 '05 #7

"Paul Schneider" <pa*******@uboot.com> wrote in message
news:c6*********@bird.wu-wien.ac.at...
Buster wrote:

No, pointers aren't excluded. Friendship is granted on a per-class
basis. Compare gets access to any Manipulator object, no matter how it's
referenced.

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'

At least give us a fighting chance. Where's line 32?
How can I get rid of this?


line 32 is:
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) );
}

P


Well Compare isn't a function or function object, its the name of a class.

Presumably you need to create a Compare object somewhere.

john
Jul 22 '05 #8
bartek wrote:
std::sort(pop.begin(), pop.end(), Compare(_func));


That seemed to work now (at least everything compiles). The working
combination is:
template<typename T, typename fitParaType, typename fitResType>
class Manipulator{
public:
[snip]
private:
class Compare{
public:
Compare(const Functor<fitParaType, fitResType>* func) : _func(func){}
bool operator () (const Genome<T, fitParaType>& arg1,
const Genome<T, fitParaType>& arg2)
{
return (_func->value(arg1.getParams())
< _func->value(arg2.getParams()));
}
private:
const Functor<fitParaType, fitResType>* _func;
};
const Functor<fitParaType, fitResType>* _func;

with implementation:

template<typename T, typename fitParaType, typename fitResType>
void Manipulator<T, fitParaType, fitResType>::sort(Population<Genome<T,
fitParaType> >& pop)
{
std::sort(pop.begin(), pop.end(), Compare(_func) );
}

Why friend didn't work remains unclear.

Thanks everybody,

Paul

Jul 22 '05 #9
Buster wrote:
Paul Schneider wrote:
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;

I'm not sure, but I think that should be "friend class
Manipulator::Compare;", so as not to forward-declare a class at
namespace scope. That's not your biggest problem, though.


The only thing that worked (meaning no compiler complaints) was :
class Manipulator{
class Compare;
friend class Compare;
...
};

The friend statement didn't have any effect, though.
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()));


[snip]

Are you sure that's necessary? You should probably not be compiling the
template definitions separately. There is an entry about this in the
FAQ.


I honestly looked for this entry by searching the FAQ for "template" and
"compile template". I didn't find anything. Where is it? Thanks for
your help.

Paul

Jul 22 '05 #10
Paul Schneider wrote:
I honestly looked for this entry by searching the FAQ for "template" and
"compile template". I didn't find anything. Where is it? Thanks for
your help.


34.12-14.

--
Regards,
Buster.
Jul 22 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by its me | last post: by
40 posts views Thread by Elijah Bailey | last post: by
20 posts views Thread by Xah Lee | last post: by
34 posts views Thread by Mark Kamoski | last post: by
11 posts views Thread by Leon | last post: by
21 posts views Thread by yeti349 | last post: by
5 posts views Thread by Jan Smith | last post: by
48 posts views Thread by Alex Chudnovsky | last post: by
7 posts views Thread by ^cypis^ vel. SQ9JTI | last post: by
1 post views Thread by CARIGAR | last post: by
9 posts views Thread by bryonone | last post: by
xarzu
1 post views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.