问题
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?
回答1:
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.
回答2:
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);
}
来源:https://stackoverflow.com/questions/44010316/operatrator-as-non-static-function