A compilation error.

T

tuko

Hello kind people.

The folliowing code gives me a compilation error, under
MSVC 6.0 and intel 8.0 compiler. It compiles fine with
g++ 3.3.1 and borland 5.5

Can you tell me please if the code is correct?.
If the code is correct do you know any tip
to "circumvent" the compilation error?

I want just to sort a list of CTimeFunction::timeFunc
objects having as key the t_ member.

Many thanks for your time.

Here is the code

// --------------------------------------------------
#include <algorithm> // Line 2
#include <list>
//
class CTimeFunction {
public:
struct timeFunc {
double t_, f_;
timeFunc () : t_(0.0), f_(0.0) {}
timeFunc (double t, double f) : t_(t), f_(f) {};
};
};
//
bool sort_by_time(const CTimeFunction::timeFunc &a, const CTimeFunction::timeFunc &b) {
return a.t_<b.t_;
}
//
int main () {
std::list<CTimeFunction::timeFunc> m_tim;
//
CTimeFunction::timeFunc mt(0.0, 1.0);
m_tim.push_back(mt); // <--- Error Here (line 24)
//
m_tim.sort(sort_by_time);
//
}

// End of Code.

Here is the error message

test.cpp(24): error: no instance of overloaded function
"std::list<_Ty, _A>::sort [with _Ty=CTimeFunction::timeFunc,
_A=std::allocator<CTimeFunction::timeFunc>]" matches the argument list
argument types are: (bool (const CTimeFunction::timeFunc &, const CT
imeFunction::timeFunc &))
object type is: std::list<CTimeFunction::timeFunc, std::allocator<CT
imeFunction::timeFunc>>
m_tim.sort(sort_by_time);
^

compilation aborted for test.cpp (code 2)
 
T

tuko

tuko wrote:

< snip >

I think I did it finally. My mind was stopped...
I overloaded the operator< and I called the sort like
m_tim.sort();
Here is the code

// --------------------------------------------------
#include <algorithm> // Line 2
#include <list>
//
class CTimeFunction {
public:
struct timeFunc {
double t_, f_;
timeFunc () : t_(0.0), f_(0.0) {}
timeFunc (double t, double f) : t_(t), f_(f) {};
};
};
//
bool sort_by_time(const CTimeFunction::timeFunc &a, const
CTimeFunction::timeFunc &b) {
return a.t_<b.t_;
}

bool operator<(const CTimeFunction::timeFunc &a,
const CTimeFunction::timeFunc &b) {
return a.t_<b.t_;
}
//
int main () {
std::list<CTimeFunction::timeFunc> m_tim;
//
CTimeFunction::timeFunc mt(0.0, 1.0);
m_tim.push_back(mt); // <--- Error Here (line 24)
//
m_tim.sort(sort_by_time);
m_tim.sort();
//
}

// End of Code.

Here is the error message

test.cpp(24): error: no instance of overloaded function
"std::list<_Ty, _A>::sort [with _Ty=CTimeFunction::timeFunc,
_A=std::allocator<CTimeFunction::timeFunc>]" matches the argument list
argument types are: (bool (const CTimeFunction::timeFunc &, const CT
imeFunction::timeFunc &))
object type is: std::list<CTimeFunction::timeFunc,
std::allocator<CT
imeFunction::timeFunc>>
m_tim.sort(sort_by_time);
^

compilation aborted for test.cpp (code 2)

Thanks for your time anyway.
 
D

David Hilsee

tuko said:
tuko wrote:

< snip >

I think I did it finally. My mind was stopped...
I overloaded the operator< and I called the sort like
m_tim.sort();

Keep in mind that the std::list::sort() implementation provided by VC++6.0
has a bug. See http://www.dinkumware.com/vc_fixes.html for more
information.

Also, unless you have a specific reason for using a std::list, I would
recommend using a std::vector and sorting it with std::sort in the header
<algorithm>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,477
Latest member
ColumbusMa

Latest Threads

Top