简体   繁体   中英

My priority_queue's objects keep registering as overflowed

Thanks for your view but this problem has been solved! I didn't have the parameters in the right order. Have a good evening!

For some reason the objects from the class named TimerEvent keep setting

error C2665: 'TimerEvent::TimerEvent' : none of the 2 overloads could convert all the argument types

But as far as I know there is no overload.

Please look at the code and let me know if you have any insight. Ty.

class TimerEvent {

public:

    char primary;
    int secondary;
    string item;
    int socketno;
    int eventnumber;
    int expirytime;
    bool eventvalid;
    TimerEvent(string td, int sno = 0, int evnum = 0, int exptime = 0, bool evvalid = false, char pri = 'A', int sec = 1)
    : primary(pri), secondary(sec), socketno(sno), eventnumber(evnum), expirytime(exptime), eventvalid(evvalid), item(td) {}

    friend bool operator<(
    const TimerEvent& x, const TimerEvent& y) {

    if(x.primary > y.primary)

        return true;
    if(x.primary == y.primary)

    if(x.secondary > y.secondary)

    return true;

    return false;
}
    friend ostream&
    operator<<(ostream& os, const TimerEvent& td) {
    return os << td.primary << td.secondary
    << ": " << td.item << td.socketno << td.eventnumber << td.expirytime << td.eventvalid;
}
};

And here's the other half

if( comsent.compare( "test" ) == 0 )    {

        timerqueue.push(TimerEvent( 'A', 2, 10, 1, 0, true, "Alright"));
                  // The above is setting the error

        cout << " Top Value Is: " << timerqueue.top().socketno << endl;
}

In the class you define the constructor to take its first argument as a string , but when you construct the object you pass a character not a string. You also pass a string as the argument pri which in the definition is a char .

It's a simple case of having to many arguments, and not remembering the exact order. Try to have a constructor with less arguments, and add functions to set values after construction. It will use more lines of code, but will be much more readable in the future.

Formally, there are two overloads for TimerEvent::TimerEvent : the constructor you defined, and the copy constructor. For overloading purposes, there are even more, since the presence of a default argument is treated like an overload. Given that, the compiler will consider the following overloads:

TimerEvent( string, int, int, int, bool, char, int )
TimerEvent( string, int, int, int, bool, char )
TimerEvent( string, int, int, int, bool )
TimerEvent( string, int, int, int )
TimerEvent( string, int, int )
TimerEvent( string, int )
TimerEvent( string )
TimerEvent( TimerEvent const& )

n the push statement, you call with:

TimerEvent( char, int, int, int, int, bool, char const[] )

Neither the first nor the last argument can match: there is no way to convert a char to either a string or a TimerEvent , and there is no way to convert a char const[] to an int. Some of the other conversions asked for are surprising as well: the fifth argument requires an int to bool conversion (unexpected for a literal), and the sixth a bool to char (very, very unexpected, resulting in either '\\0' or '\\1' ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM