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

2

8

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;

}

Millepede answered 9/6, 2017 at 10:39 Comment(6)
Does the error disapear, if you remove the '+' in the 6 line?Lepus
would anyone suggest some useful tips on how to proceed further to gain expertise in C++. I know the basics as I studied it for class 12 boards, from Sumita Arora.Millepede
@manni66 sorry there isnt any + that is a typo. the error is on line 8Millepede
Could you remove this error block or at least format it? It is useless as it is.Pacifist
Also, Matrix[i,j] has to be written as Matrix[i][j]. There is no operator[] taking two parameters.Cateyed
everything works fine now. thanks for the help everyone. also Im new to stackoverflow. it was a great help.Millepede
P
20

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);
Pacifist answered 9/6, 2017 at 10:58 Comment(1)
I see... I always ran into this error. Thank you!Deguzman
I
0

I'm confronted with the same error in my code. However, the mistake is different from the sample code. I tried a lot to fix it. Thus, I post my solution here in case someone needs it.

My error occurs when I'm trying to insert a lower dimensional vector (e.g. vector<uint32_t> a) to a higher dimensional vector (e.g. vector<vector<uint32_t>> b) by

 `b.insert(b.begin(), a.begin(), a.end());`

However, this invocation is wrong. According to the error information after compilation, I think the error lies in that the address of a does not match with the address of b, where their dimensions are different.

To fix the error, I simply change the code to b.insert(b.begin(), a);

which runs well. To insert a part of a to b, you can just create a new variable containing what you want to insert, and replace a with that.

Idolum answered 19/8 at 7:57 Comment(1)
Yes, this is very different from the question's example. You look to be calling (std::vector<std::vector<uint32_t>>::)insert(const_iterator __position, _InputIterator __first, _InputIterator __last) with _InputIterator as std::vector<uint32_t>::const_iterator (or just iterator). It's uncertain whether std::_RequireInputIter applies, so which variant of insert exactly is not certain.Slot

© 2022 - 2024 — McMap. All rights reserved.