No match for 'operator!='

I have this code and I keep getting the above mentioned error at line
1
2
3
if(A.size() != B.size()){
   A.size() = B.size();
}


what is the problem here? Why can't I assign things like this with template code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35


#include <iostream>
#include <vector>
using namespace std;

template <typename T>
void copy(vector<T> B, vector<T> A){
	vector<int>::iterator a1;
	vector<int>::iterator b1;
	
	if(A.size() != B.size()){
		A.size() = B.size();
	}
	
	for(b1 = B.begin(); b1 != B.end(); b1++){
		*b1 = *a1;
		a1++;
	}
	
	for(a1 = A.begin(); a1 != A.end(); a1++){
		cout << *a1 << endl;
	}
}

int main(int argc, char *argv[]) {
	vector<float> V1, V2;
	float input;
	while (cin >> input)
		V1.push_back(input);
	copy(V2, V1);
}


I don't know what else but
a.Size() = B.size() seems illegal unless a.Size returns a reference. I don't think it does here.

vector already has a copy ... you can just assign x=y for that matter. No reason why you can't play with it, I guess...

https://www.cplusplus.com/reference/algorithm/copy/
copy is already used in std namespace. If your compiler found it, that could cause no end of woes.
Last edited on
A.size() = B.size(); is without a doubt illegal. std::vector has a resize() member that can modify the size of the vector.
But, as jonnin said, std::vector already provides a copy constructor and assignment operator that deep-copies the elements.
ah I see I will use resize now. I know stl has copy but I want to do it this way. Thank you

I will mention though, all of this works when i remove the template.
Last edited on
To copy the contents of vector a to vector b:
 
b = a;


I will mention though, all of this works when i remove the template.


Seems quite unlikely
Last edited on
Why can't I assign things like this with template code?
Take a look at line 9/10. You have an int there where you should have T.

Apart from the resize(...) problem.
You said:
No match for 'operator!='

but you did not tell where.

In this case the where is lines 16 and 21. In fact, those lines have also:
No match for 'operator='


If we look at line 16: for(b1 = B.begin(); b1 != B.end(); b1++) with the types of objects:
1
2
3
4
5
6
void copy(vector<T> B, vector<T> A){
    vector<int>::iterator b1;
    for ( b1 = B.begin(); b1 != B.end(); b1++ )

vector<float> V1, V2;
copy( V2, V1 );

we see that the operands on both cases are vector<int>::iterator and vector<float>::iterator,
then we reach to the explanation.

Or, as a compiler says:
16:9: error: no match for 'operator=' (operand types are 'std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}' and 'std::vector<float>::iterator {aka __gnu_cxx::__normal_iterator<float*, std::vector<float> >}')
16:25: error: no match for 'operator!=' (operand types are 'std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}' and 'std::vector<float>::iterator {aka __gnu_cxx::__normal_iterator<float*, std::vector<float> >}')


Focus on these:
* Where does the (syntax) error occur?
* What is in there?
* What does the compiler tell?
Topic archived. No new replies allowed.