I got a piece of code as follows:
template <typename T>
class Ordered_list{
public:
Ordered_list(bool (*less_than_function)(const T&, const T&)): less_than(less_than_function){}
private:
bool (*less_than) (const T&, const T&);
};
bool less_than_f(const int& i1, const int& i2){
return i1 < i2;
}
class Collection{
public:
Ordered_list<int> list(less_than_f);
};
The code doesn’t compile, and I got the error:
test.cpp:16: error: 'less_than_f' is not a type
I was trying to create a object for the Ordered_list class, which I need to pass in a function pointer. I tried to google for the reason, but didn’t get much. Can anybode tell me what should I do to make it work?
Here you are declaring a function with return
Ordered_list<int>and take a nonamed parametr of typeless_than_f(Opss! no type), while you want to have a member “list“. You need to initializate it in the initialization list of the constructor (becauseOrdered_listdont have a default constructor) [hmm.. not sure about C++11 new options to initialize, but not now in VC2012].