Compile problem: sort() cannot be used as a function!?

This is a discussion on Compile problem: sort() cannot be used as a function!? within the C++ Programming forums, part of the General Programming Boards category; Hi, I am doing a project comparing run times of STL sorts to sorts i have written. I am having ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    89

    Compile problem: sort() cannot be used as a function!?

    Hi, I am doing a project comparing run times of STL sorts to sorts i have written. I am having a really weird compiler error that looks like this:

    Code:
    main.cc: In function 'int main(int, char**)':
    main.cc:56: error: 'sort' cannot be used as a function
    Here is some of the code from my main:
    Code:
    switch(sort){
             case 'q':
                if(stl)
                   sort(my_vect.begin(), my_vect.end()); 
                else
                   quick_sort(my_vect);
                break;
             case 'm':
                if(stl)
                   stable_sort(my_vect.begin(), my_vect.end());
                else
                   merge_sort(my_vect);
                break;
    ...
    when i comment out the line that calls sort() everything works fine... even stable_sort which is from the same library and uses the same parameters. Also i have another small program just one file that calls sort() and it works there fine. This is completely insane and i cannot figure it out and any hints would be great.
    thanks

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,815
    Code:
    switch(sort){
             case 'q':
                if(stl)
                   sort(my_vect.begin(), my_vect.end());
    Possible confusion due to variable name being similar to function name? Try changing the name of the variable in your code to something else.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,628
    Code:
    switch(sort){
    Apparently you have a variable named sort. I suggest that you dont have a using namespace std directive or a using std::sort directive, but instead use the fully qualified names.

    Code:
    switch(sort){
             case 'q':
                if(stl)
                   std::sort(my_vect.begin(), my_vect.end()); 
                else
                   quick_sort(my_vect);
                break;
             case 'm':
                if(stl)
                   std::stable_sort(my_vect.begin(), my_vect.end());
                else
                   merge_sort(my_vect);
                break;
    ...
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21