no match for 'operator='(operand type are 'std::vector<int>' and 'int'

26,966

The following:

vector<int> v[100];

...declares 100 vectors of int with size 0, so when you do:

v[i] = i+1;

v[i] is the ith vector, so you try to assign i + 1 which is a int to v[i] which is vector.

If you want a single vector of size 100, the correct syntax is:

vector<int> v(100);
Share:
26,966
Sohit Gore
Author by

Sohit Gore

jack of all traits, master of none.

Updated on July 09, 2022

Comments

  • Sohit Gore
    Sohit Gore almost 2 years

    I'm new to c++ and I'm learning about vector. I wrote a basic code to see how it works but i get the following errors.

    ||=== Build file: "no target" in "no project" (compiler: unknown) ===| C:\Users\Sohit\Desktop\Comparator\vectors.cpp||In function 'int main()':| C:\Users\Sohit\Desktop\Comparator\vectors.cpp|7|error: 'void operator=(const int&)' must be a nonstatic member function| C:\Users\Sohit\Desktop\Comparator\vectors.cpp|10|error: no match for 'operator=' (operand types are 'std::vector' and 'int')|

    no match for 'operator='(operand type are 'std::vector' and 'int' please help

    #include<iostream>
    #include<vector>
    using namespace std;
    int main()
    {
    vector<int> v[100];
    for(int i=0;i<100;i++)
        v[i]=i+1;
    
        int Count=v.size();
        std::cout<<Count;
        bool is_nonempty=!v.empty();
    
        cout<<is_nonempty;
    
    
        vector<int>v2;
    for(int i=0;i<100;i++)
        v2.push_back(i*i);
    
    int count2 = v2.size();
    cout<<count2;
    
    v.resize(200);
    
    for(int i=100;i<200;i++)
        v[i]=i*i*i;
    
    v.resize(200);
    
    for(int i=100;i<200;i++)
        v2.push_back(i*i*i);
    
    vector<int> v3=v2;
    
    v.clear();
    
    v(20,"unknown");
    
    int n,m;
    cin>>n>>m;
    vector< vector<int> > Matrix(n,vector<int>(m,-1));
    
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        cout<<Matrix[i][j];
    
    return 0;
    

    }