5 Replies - 2035 Views - Last Post: 17 March 2013 - 02:51 PM Rate Topic: -----

#1 krillov  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 18-November 12

Problem with sort algorithm

Posted 17 March 2013 - 06:12 AM

Hi,


Iīd like to use sort algorithm in my program, but somehow it doesnīt wanna work. I get this error message in Visual Studio 2010:

...\projekt\jukebox.cpp(260): error C3867: 'Jukebox::sortAlbum': function call missing argument list; use '&Jukebox::sortAlbum' to create a pointer to member
...\projekt\jukebox.cpp(260): error C2780: 'void std::sort(_RanIt,_RanIt)' : expects 2 arguments - 3 provided
e:\instprograms\msvisualstudio\vc\include\algorithm(3639) : see declaration of 'std::sort'


void Jukebox::sortAlbumByName()
{
	std::sort(albums.begin(), albums.end(), sortAlbum);
}

bool Jukebox::sortAlbum(const Album &pAlbum1, const Album &pAlbum2)
{
	std::string album1 = pAlbum1.getAlbumTitle();							
	std::string album2 = pAlbum2.getAlbumTitle();

	std::transform(album1.begin(), album1.end(), album1.begin(), (int (*)(int))tolower);
	std::transform(album2.begin(), album2.end(), album2.begin(), (int (*)(int))tolower);

	return ((album1.compare(album2)) == -1);	
}



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with sort algorithm

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,117
  • Joined: 05-May 12

Re: Problem with sort algorithm

Posted 17 March 2013 - 09:04 AM

This error:

View Postkrillov, on 17 March 2013 - 09:12 AM, said:

[color="#FF0000"]...\projekt\jukebox.cpp(260): error C3867: 'Jukebox::sortAlbum': function call missing argument list; use '&Jukebox::sortAlbum' to create a pointer to member

is referring to
03    std::sort(albums.begin(), albums.end(), sortAlbum);



You declared your function sortAlbum() as an instance method. Change it to a static method and things should work better.
Was This Post Helpful? 2
  • +
  • -

#3 krillov  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 18-November 12

Re: Problem with sort algorithm

Posted 17 March 2013 - 10:32 AM

I tried to change to a static method but it didnīt work or maybe I did it wrong. I moved the function outside of the class declaration and itīs working now but I donīt know if itīs an acceptable solution. Any idea? I got the same problems when I try to use for_each algorithm.
Was This Post Helpful? 0
  • +
  • -

#4 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Problem with sort algorithm

Posted 17 March 2013 - 01:23 PM

std::sort takes two arguments in general, you're passing it 3, but I believe you're passing the third argument incorrectly. Have a look at this reference for std::sort
Was This Post Helpful? 1
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2114
  • View blog
  • Posts: 3,240
  • Joined: 21-June 11

Re: Problem with sort algorithm

Posted 17 March 2013 - 01:36 PM

View PostIngeniousHax, on 17 March 2013 - 09:23 PM, said:

std::sort takes two arguments in general


It has one overload that takes two arguments and one that takes three.

Quote

but I believe you're passing the third argument incorrectly.


Other than the fact that, as Skydiver pointed out, the function needs to be static (or free) in order to be referenced in that way, there's nothing wrong with how the OP passes the third argument.

View Postkrillov, on 17 March 2013 - 06:32 PM, said:

I tried to change to a static method but it didn´t work or maybe I did it wrong.



Well, how did you do it and what was the error that you got?

This post has been edited by sepp2k: 17 March 2013 - 01:37 PM

Was This Post Helpful? 1
  • +
  • -

#6 krillov  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 18-November 12

Re: Problem with sort algorithm

Posted 17 March 2013 - 02:51 PM

I fixed. I made a dumb mistake and wrote static in the cpp file and not in the header file only. It works correctly now with three arguments. Thanks everyone. :-)

This post has been edited by krillov: 17 March 2013 - 02:53 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1