Merge Sort C++ Compile Error

I'm trying to implement a merge sort function and I'm getting a compiler error that says "no match for 'operator=' " It occurs on my recursive assignment of left_list = mergesort(left_list) and the same error in the following line using right_list.

It compiles correctly if I take out the assignment and just have mergesort(left_list) but then it doesn't sort correctly. I thought that the way I have it now should merge sort correctly, but given that it doesn't, either the error has something to do with those lines or it's elsewhere in the mergesort() or merge() function.

Any help would be appreciated.

EDIT: I should add that this is a homework assignment and I was required to implement the code inside the function so the function "mergesort" is void even though I think it should return a vector, but my professor's online unit test grading system is counting on the fact that the function is "void"...
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
    void mergesort(vector<int> &data) {
	
	vector<int> left_list;
	vector<int> right_list;

	if ( data.size() <= 1 ) {
		return;
	}
	
	// creates a middle point to separate into 2 sub lists
	int middle =  ( data.size() / 2 );
	
	// create a list of elements to the left of middle
	for ( int i = 0; i < middle; i++ ) {
		left_list.push_back(data[i]);
	}
	
	// create a list of elements to the right of middle
	for ( unsigned int i = middle; i < data.size(); i++ ) {
		right_list.push_back(data[i]);
	}
	
	// break down the sub lists until they are of size 1
	left_list = mergesort(left_list);
	right_list = mergesort(right_list);
	
	// merge the sublists in the correct order
	merge(left_list, right_list);
	
	

    }

    vector<int> merge(vector<int> &left, vector<int> &right) {
 
	vector<int> result;
	
	unsigned left_it = 0;
	unsigned right_it = 0;

    while( left_it < left.size() && right_it < right.size() ) {
		
        
        // the smaller value is put into the result vector
        if( left[left_it] < right[right_it] ) {
			
            result.push_back(left[left_it]);
            left_it++;
        }
        else
        {
            result.push_back( right[right_it] );
            right_it++;
        }
    }

    // Put the rest of the data from both vectors onto result
    while( left_it < left.size() ) {
		
        result.push_back( left[left_it] );
        left_it++;
    }

    while( right_it < right.size() ) {
		
        result.push_back( right[right_it] );
        right_it++;
    }

    return result;
  
    }
Last edited on
you know that in :
left_list = mergesort(left_list);
vector<int> can't assigned with void return function.

if you want to keep mergesort function with void type, try this :
1
2
3
4
5
void mergesort(vector<int> &data)
{
data = mergeoperation(data);
// prints the output
}
Topic archived. No new replies allowed.