0

Hi. I'm trying to overload read data from a file and re-order a queue based on the data. This is the code I have so far:

Main()

priority_queue <Event> queue;

while (inFile2 >> INPUT) {

    if (INPUT == "INPUT") {

        inFile2 >> wrname >> time >> value; 

        Event *event = new Event(wrname, time, value);
        queue.push(*event);

    }

The Event Class Header:

#ifndef EVENT_H
#define EVENT_H 
#include <string>

class Event
{
public:
    Event();
    Event(std::string, int, char); 
    bool operator<(const Event &)const;
    int getTime(); 
    ~Event();
    void execute(); 

private: 
    int time = 0; 
    char val; 
    static int tiebreaker; 

};

The Event Class Implementation:

Event::Event(std::string n1, int t1, char v1)
{
    tiebreaker++; 
    time = t1; 
    val = v1; 

}

bool Event::operator<(const Event & ev)const
{
    if (time > ev.time) {
        return true;
    }
    else if (time < ev.time) {
        return false;
    }
    else {
        if (tiebreaker > ev.tiebreaker) {
            return true;
        }
        else {
            return false;
        }

    }
}

The compilation error is returning the error: '<': no operator found which takes a left-hand operand of type 'const Event' (or there is no acceptable conversion). When I change the function to const, it shows an error: "unresolved external symbol "private: static int Event::tiebreaker" (?tiebreaker@Event@@0HA)"

Joe
  • 7
  • 5
  • Make the member function a `const` member function. – R Sahu Dec 11 '17 at 05:46
  • Take a look this SO answer. https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading/4421719#4421719. Pay attention to the section titled **Comparison operators**. – R Sahu Dec 11 '17 at 05:48
  • I tried fixing to a const, but it returns an unresolved external symbol. The functions used for Event class all have function body so I'm more confused now. – Joe Dec 11 '17 at 05:54
  • The linker error is related to the `static` member variable `tiebreaker`. You can define it adding a line `int Event::tribreaker = 0;` in the .cc file. – R Sahu Dec 11 '17 at 06:03
  • I had thought that the problem was in the initialization but couldn't figure out how to initialize a static variable. This solved it. Thanks – Joe Dec 11 '17 at 19:14

0 Answers0