473,395 Members | 1,418 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 1702

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
34
by: Mark Kamoski | last post by:
Hi-- Please help. I need a code sample for bubble sort. Thank you. --Mark
11
by: Leon | last post by:
I have six textbox controls on my webform that allows the user to enter any numbers from 1 to 25 in any order. However, I would like to sort those numbers from least to greatest before sending them...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
5
by: Jan Smith | last post by:
I've searched the overloads for the Array.Sort method, and I haven't found a clear answer to my question. Maybe it's not in Array.Sort. Here's the question: I initialize an array X with the...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
7
by: ^cypis^ vel. SQ9JTI | last post by:
Hi, i need your help. I have to prepare a homework, easy program, which will be sorting the values from txt file and writing the solution to another txt file. It has to be a bucket sort. Have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.