0

Code:

SchedulingItem operator[](Schedule obj,int el){
    return obj.OfVector().at(el);
}

Error:

academia::SchedulingItem academia::operator[](academia::Schedule, int)' must be a nonstatic member function
     SchedulingItem operator[](Schedule obj,int el)

Where is the problem?

SpeedClimber
  • 29
  • 1
  • 7

2 Answers2

5

The problem is that, just as the message says, this function must be a non-static member function.

That's simply a law of C++, for operator[].

You've instead made it a non-member, or "free" function.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

operator[] must be a non-static member of your Schedule class, eg:

class Schedule
{
private:
    std::vector<SchedulingItem> m_vec;
public:
    SchedulingItem& operator[](int el);
};

SchedulingItem& Schedule::operator[](int el)
{
    return m_vec.at(el);
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770