473,806 Members | 2,319 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL sort question

I want to sort a class derived from std::vector with STL sort.:
template<typena me 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<fitPara Type, fitResType>* _func;

class Compare{
public:
Compare(){}
bool operator () (Genome<T, fitParaType>& arg1,
Genome<T, fitParaType>& arg2)
{
return (_func->value(arg1.get Params())
< _func->value(arg2.get Params()));
}
};
};

template<typena me T, typename fitParaType, typename fitResType>
void Manipulator<T, fitParaType, fitResType>::so rt(Population<G enome<T,
fitParaType> >& pop)
{
// Population is derived from std::vector
std::sort(pop.b egin(), pop.end(), Manipulator<T, fitParaType,
fitResType>::Co mpare() );
}
// instantiation, so it compiles
template class Manipulator<Rea lGene<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<Rea lGene<double>, double,
double>::Compar e' 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 1740

"Paul Schneider" <pa*******@uboo t.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<typena me 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<Rea lGene<double>, double,
double>::Compar e' 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<typena me 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<typena me 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::Co mpare;", 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<fitPara Type, fitResType>* _func;

class Compare{
public:
Compare(){}
bool operator () (Genome<T, fitParaType>& arg1,
Genome<T, fitParaType>& arg2)
{
return (_func->value(arg1.get Params())
< _func->value(arg2.get Params()));
A Manipulator::Co mpare 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 lexicographical ly -- so common, in fact, that you can compare two
std::vectors with < or std::less.
}
};
};

template<typena me T, typename fitParaType, typename fitResType>
void Manipulator<T, fitParaType, fitResType>::so rt(Population<G enome<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.b egin(), pop.end(), Manipulator<T, fitParaType,
fitResType>::Co mpare() );
}
// instantiation, so it compiles
template class Manipulator<Rea lGene<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<Rea lGene<double>, double,
double>::Compar e' 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*******@uboo t.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<type name 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<Rea lGene<double>, double,
double>::Compar e' 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<typena me 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<fitPara Type, fitResType>* func) : _func(func){}
bool operator () (Genome<T, fitParaType>& arg1,
Genome<T, fitParaType>& arg2)
{
return (_func->value(arg1.get Params())
< _func->value(arg2.get Params()));
}
private:
const Functor<fitPara Type, fitResType>* _func;
};

with implementation of the sort function:

template<typena me T, typename fitParaType, typename fitResType>
void Manipulator<T, fitParaType, fitResType>::so rt(Population<G enome<T,
fitParaType> >& pop)
{
std::sort(pop.b egin(), pop.end(), this->Compare(_fun c) );
}

This gives me only one error:
.../Manipulator.cpp :32: error: call to non-function `class
Manipulator<Rea lGene<double>, double, double>::Compar e'

How can I get rid of this?

Thanks again,

Paul

Jul 22 '05 #4
Paul Schneider wrote:
John Harrison wrote:
"Paul Schneider" <pa*******@uboo t.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<typena me 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<Rea lGene<double>,
double,
double>::Compar e' 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<typena me 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<fitPara Type, fitResType>* func) :
_func(func){}
bool operator () (Genome<T, fitParaType>& arg1,
Genome<T, fitParaType>& arg2)
{
return (_func->value(arg1.get Params())
< _func->value(arg2.get Params()));
}
private:
const Functor<fitPara Type, fitResType>* _func;
};

with implementation of the sort function:

template<typena me T, typename fitParaType, typename fitResType>
void Manipulator<T, fitParaType, fitResType>::so rt(Population<G enome<T,
fitParaType> >& pop)
{
std::sort(pop.b egin(), pop.end(), this->Compare(_fun c) );
}

This gives me only one error:
../Manipulator.cpp :32: error: call to non-function `class
Manipulator<Rea lGene<double>, double, double>::Compar e'
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*******@uboo t.com> wrote in
news:c6******** *@bird.wu-wien.ac.at:
John Harrison wrote:
"Paul Schneider" <pa*******@uboo t.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<typ ename 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<Rea lGene<double>,
double,
double>::Compar e' 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<typena me 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<fitPara Type, fitResType>* func) :
_func(func){} bool operator () (Genome<T, fitParaType>&
arg1,
Genome<T, fitParaType>& arg2)
{
return (_func->value(arg1.get Params())
< _func->value(arg2.get Params()));
}
private:
const Functor<fitPara Type, fitResType>* _func;
};

with implementation of the sort function:

template<typena me T, typename fitParaType, typename fitResType>
void Manipulator<T, fitParaType,
fitResType>::so rt(Population<G enome<T, fitParaType> >& pop)
{
std::sort(pop.b egin(), pop.end(), this->Compare(_fun c) );
}

This gives me only one error:
../Manipulator.cpp :32: error: call to non-function `class
Manipulator<Rea lGene<double>, double, double>::Compar e'

How can I get rid of this?


std::sort(pop.b egin(), 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<fitPara Type, fitResType>* func) :
_func(func){}
bool operator () (Genome<T, fitParaType>& arg1,
Genome<T, fitParaType>& arg2)
{
return (_func->value(arg1.get Params())
< _func->value(arg2.get Params()));
}
private:
const Functor<fitPara Type, fitResType>* _func; };

with implementation of the sort function:

template<typena me T, typename fitParaType, typename fitResType>
void Manipulator<T, fitParaType,
fitResType>::so rt(Population<G enome<T, fitParaType> >& pop)
{
std::sort(pop.b egin(), pop.end(), this->Compare(_fun c) );
}

This gives me only one error:
../Manipulator.cpp :32: error: call to non-function `class
Manipulator<Rea lGene<double>, double, double>::Compar e'

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


line 32 is:
template<typena me T, typename fitParaType, typename fitResType>
void Manipulator<T, fitParaType, fitResType>::so rt(Population<G enome<T,
fitParaType> >& pop)
{
std::sort(pop.b egin(), pop.end(), this->Compare(_fun c) );
}

P

Jul 22 '05 #7

"Paul Schneider" <pa*******@uboo t.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<fitPara Type, fitResType>* func) :
_func(func){}
bool operator () (Genome<T, fitParaType>& arg1,
Genome<T, fitParaType>& arg2)
{
return (_func->value(arg1.get Params())
< _func->value(arg2.get Params()));
}
private:
const Functor<fitPara Type, fitResType>* _func; };

with implementation of the sort function:

template<typena me T, typename fitParaType, typename fitResType>
void Manipulator<T, fitParaType,
fitResType>::so rt(Population<G enome<T, fitParaType> >& pop)
{
std::sort(pop.b egin(), pop.end(), this->Compare(_fun c) );
}

This gives me only one error:
../Manipulator.cpp :32: error: call to non-function `class
Manipulator<Rea lGene<double>, double, double>::Compar e'

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


line 32 is:
template<typena me T, typename fitParaType, typename fitResType>
void Manipulator<T, fitParaType, fitResType>::so rt(Population<G enome<T,
fitParaType> >& pop)
{
std::sort(pop.b egin(), pop.end(), this->Compare(_fun c) );
}

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.b egin(), pop.end(), Compare(_func)) ;


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

with implementation:

template<typena me T, typename fitParaType, typename fitResType>
void Manipulator<T, fitParaType, fitResType>::so rt(Population<G enome<T,
fitParaType> >& pop)
{
std::sort(pop.b egin(), 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<typena me 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::Co mpare;", 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<fitPara Type, fitResType>* _func;

class Compare{
public:
Compare(){}
bool operator () (Genome<T, fitParaType>& arg1,
Genome<T, fitParaType>& arg2)
{
return (_func->value(arg1.get Params())
< _func->value(arg2.get Params()));


[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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3770
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
4341
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 number of records. Both these numbers are known
20
4088
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: li=;
10
15137
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 *enemydata; // Holds information about an enemy (in a game) // Its a double linked list node
34
7334
by: Mark Kamoski | last post by:
Hi-- Please help. I need a code sample for bubble sort. Thank you. --Mark
11
1949
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 to the database. How can accomplish this task? Thanks!
21
3229
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 column. Once the array elements are split, what is the best way to sort them? Thank you. //populate data object with data from xml file. //Data is a comma delimited list of values var jsData = new Array(); jsData = {lib: "#field...
5
2844
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 values 28 142 3 17 225. I can sort this array in ascending order and it will return 3 17 28 142 225. But I want a method that will return the sort order, not the array in sorted
48
4494
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 lot faster with both methods using .NET 1.1, that's why I am pretty sure its a (rather serious) bug. Below you can find C# test case that should allow you to reproduce this error, to run it you will need to put 2 data files into current directory...
7
5066
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 anyone a source code for this sample? Many thanks in advance! Regards, Luke
0
9597
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10618
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10371
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10110
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9187
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.