Connecting Tech Pros Worldwide Help | Site Map

A compilation error.

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 04:38 PM
tuko
Guest
 
Posts: n/a
Default A compilation error.

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)

--
tuko, the mexican - the ugly fellow

  #2  
Old July 22nd, 2005, 04:38 PM
tuko
Guest
 
Posts: n/a
Default Re: A compilation error.

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();
[color=blue]
>
> 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_;
> }[/color]

bool operator<(const CTimeFunction::timeFunc &a,
const CTimeFunction::timeFunc &b) {
return a.t_<b.t_;
}
[color=blue]
> //
> 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);[/color]

m_tim.sort();[color=blue]
> //
> }
>
> // 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)
>[/color]

Thanks for your time anyway.

--
tuko, the mexican - the ugly fellow

  #3  
Old July 22nd, 2005, 04:39 PM
David Hilsee
Guest
 
Posts: n/a
Default Re: A compilation error.


"tuko" <tuko@away.com> wrote in message news:cdh267$iaq$1@nic.grnet.gr...[color=blue]
> 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();
>[/color]

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>.

--
David Hilsee


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.