lundi 6 septembre 2021

Error: No viable overloaded operator[] for type vector

Hi I am trying to practice c++ and came across a error while solving a problem, I am using vectors in my code and getting error where I added two elements in vector through reference operator'[]'

C++

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> temp;
        int p = 0;
        for (auto i = nums.begin(); i != (nums.end() - 1); ++i) {
            cout<<*i;
            p = nums[i]+nums[i+1];                 //Error is in this line
            if ((p) == target) {
                temp.push_back(i);
                temp.push_back(i + 1);
                return temp;
            }
        }
    }
};

Error: No viable overloaded operator[] for type vector

Aucun commentaire:

Enregistrer un commentaire