Sorting vectors error

Need help figuring out why my code is having an issue, I'm sorting and mergeing two vectors but I'm getting an error with my function name I have it commented where the error is showing up. Can someone please explain why?



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
 
using namespace std;

vector<int> merge_sorted(vector<int> a, vector<int> b)
{
 int i = 0, j = 0, p;

 do
 {
	 if( a[i]< b[j])
	 {
		merge_sorted.push_back( a[i]);// I'm having errors with the merge_sorted part it says " error C2228: left of '.push_back' must have class/struct/union"

       	i += 1;
	 }
	 else
	 {
		merge_sorted.push_back = (b[j]);
		j += 1;
	 }   			
 }
 while ( i < a.size && j < b.size);
 

 if (i < a.size)
   for (p = i; p < a.size; p++)

	merge_sorted.push_back (a[p]); 

else
     for(p = j; p < a.size; p++)
	merge_sorted.push_back (b[p]);
}

Last edited on
merge_sorted() is a function, not a class/structure/union. The way you have it written you should be using it like this (unfamiliar with templates as I'm fairly new myself so not certain about how to declare vectors):
1
2
3
vector<int> vec_1, vec_2, vec_2;
//set vec_2 and vec_3 to some values
vec_1 = merge_sorted(vec_2, vec_3);


Assuming push_back() is a member function of the vector class you use it like this:
*something, maybe nothing* = vec_1.push_back(*argument(s)*)
Ok I'll try it thanks
Topic archived. No new replies allowed.