Why can't I overload operator<< on leetcode?

Let's say when I am writing program, sometimes I want to see the variable values. But when I try to overload << operator the leetcode system complains by saying that there is an error.

For example this code:

class Solution {
public:
    template <typename T>
    friend ostream& operator<<(ostream &out, const vector<T> &v);

    int maxProfit(vector<int>& prices) {
        int n = prices.size();
        vector<vector<int>> indices;
        for(int i = 0 ; i < n; i++)
            indices[i] = vector<int>{prices[i],i}; 
        sort(indices.begin(), indices.end(), 
             [](const vector<int> &u, const vector<int> &v){ 
                 return (u[0] >= v[0]); 
             });
        cout << indices << endl;
        return 0;
    }
};

template <typename T>
ostream& operator<< (ostream &out, const vector<T> &v) {
    out << "]";
    int n = v.size();
    if(n > 0) {
        for(int i = 0 ; i < n-1; i++) out << v[i] << ", ";
        out << v[n-1];
    }
    out << "]";
    return out;
}

and I am getting this error:

Line 15: Char 14: error: invalid operands to binary expression ('std::ostream' (aka 'basic_ostream<char>') and 'vector<vector<int>>')
        cout << indices << endl;
        ~~~~ ^  ~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/cstddef:130:5: note: candidate function template not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'std::byte' for 1st argument
    operator<<(byte __b, _IntegerType __shift) noexcept

why?

Comments (0)