1

I'm currently writing a program to detect license plate text. I have a class PossiblePlate with member variables, at one point in main I would like to sort a vector of PossiblePlate objects in descending order by the number of characters detected in the plate, which is in the member variable strChars. Here is the relevant code:

in main:

std::sort(vectorOfPossiblePlates.begin(), vectorOfPossiblePlates.end(), PossiblePlate::sortDescendingByNumberOfChars);

PossiblePlate.h (so far, I'm probably going to add more):

// PossiblePlate.h

#ifndef POSSIBLEPLATE_H
#define POSSIBLEPLATE_H

#include <string>

#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>

///////////////////////////////////////////////////////////////////////////////////////////////////
class PossiblePlate {
public:
    // member variables ///////////////////////////////////////////////////////////////////////////
    cv::Mat imgPlate;
    cv::Mat imgGrayscale;
    cv::Mat imgThresh;

    std::vector<cv::RotatedRect> locationOfPlateInScene;

    std::string strChars;

    ///////////////////////////////////////////////////////////////////////////////////////////////////
    bool sortDescendingByNumberOfChars(const PossiblePlate &ppLeft, const PossiblePlate &ppRight);

};

#endif      // end #ifndef POSSIBLEPLATE_H

here is PossiblePlate.cpp (so far, I may add more)

// PossiblePlate.cpp

#include "PossiblePlate.h"


///////////////////////////////////////////////////////////////////////////////////////////////////
bool sortDescendingByNumberOfChars(const PossiblePlate &ppLeft, const PossiblePlate &ppRight) {
    return(ppLeft.strChars.length() < ppRight.strChars.length());
}

When I run this with Visual Studio 2013 I get the following error:

Error   1   error C3867: 'PossiblePlate::sortDescendingByNumberOfChars': function call missing argument list; use '&PossiblePlate::sortDescendingByNumberOfChars' to create a pointer to member c:\visualstudio2013progs\cpp\licenseplaterecognition1\main.cpp  44  1   LicensePlateRecognition

Line 44 in main is the line in main above that calls std::sort.

I've done the same thing in other programs before without a problem. Everything I'm doing here is consistent with how I have done this in the past and with other Stack Overflow posts such as these:

sorting a vector of structs

sort vector of struct element

Sorting a vector of objects in C++

Can anybody tell me what I'm doing wrong? I'm at a loss here, any help would be greatly appreciated.

1 Answer 1

4

There are several problems in the posted code.

First, the function passed to std::sort has to be callable with two arguments of the type being sorted. PossiblePlate::sortDescendingByNumberOfChars is a non-static member function, so it has to be called on an object of type PossiblePlate, with two additional arguments. std::sort can't cope with that. Make it a static member function.

Second, PossiblePlate.cpp defines a free function named sortDescendingByNumberOfChars; that's okay, but it's not the member function. Change its declaration from sortDescendingByNumberOfChars to PossiblePlate::sortDescendingByNumberOfChars to define the member function.

Third, follow the advice in the error message. To take the address of a member function, the correct syntax is &ClassName::FunctionName. The call in the code does not have the &.

Sign up to request clarification or add additional context in comments.

3 Comments

As answered, std::sort won't work with non-static member functions. Alternatives to using a static function are a lambda function or a functor (function operator).
@rcgldr - or with a free function, like the one defined in PossiblePlate.cpp. But that's not what this question is about.
I just wanted to warn the OP about the potential issue and alternatives.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.