Trying to make a selection sort for my user class array

I am trying to sort my array of data type "Invoice" in ascending order by partDes (the string element). I am getting alot of errors, but I feel like I am close. Any advice is appreciated!

Error:
Severity Code Description Project File Line Suppression State
Error C2665 'swap': none of the 4 overloads could convert all the argument types U14 c:\users\christian\source\repos\u14\u14\u14.cpp 65


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
73
74
75
76
// U14.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include <iomanip>
#include "Invoice.h"

using namespace std;

void selectionSort(Invoice[], int);
void swap(int &, int &);

int main()
{
	const int NUM_ITEMS = 8;
	Invoice invoices[] =
	{
		Invoice(83, "Electric sander", 7, 57.98),
		Invoice(24, "Power saw", 18, 99.99),
		Invoice(7, "Sledge hammer", 11, 21.5),
		Invoice(77, "Hammer", 76, 11.99),
		Invoice(39, "Lawn mower", 3, 79.5),
		Invoice(68, "Screwdriver", 106, 6.99),
		Invoice(56, "Jig saw", 21, 11.00),
		Invoice(3, "Wrench", 34, 7.5)
	};

	cout << "Part Number\t";
	cout << setw(7) << "Part Description\t";
	cout << setw(7) << "Quantity\t";
	cout << setw(7) << "Price\n";
	cout << "----------------------------------------------------------------\n";

	for (int i = 0; i < NUM_ITEMS; i++)
	{
		cout << invoices[i].getNum();
		cout << setw(11) << "\t" << invoices[i].getDes();
		cout << setw(11) << "\t" << invoices[i].getQuan();
		cout << setprecision(2) << fixed << setw(11) << "\t$" << invoices[i].getPrice() << endl;
	}

	selectionSort(invoices, NUM_ITEMS);

	return 0;
}

void selectionSort(Invoice invoices[], int NUM_ITEMS)
{
	string minValue;
	int minIndex;

	for (int start = 0; start < (NUM_ITEMS - 1); start++)
	{
		minIndex = start;
		minValue = invoices[start].getDes();

		for (int index = start + 1; index < NUM_ITEMS; index++)
		{
			if (invoices[index].getDes() < minValue)
			{
				minValue = invoices[index].getDes();
				minIndex = index;
			}
		}
		swap(invoices[minIndex].getDes(), invoices[start].getDes());
	}
}

void swap(int &a, int &b)
{
	int temp = a;
	a = b;
	b = temp;
}
Last edited on
You have trouble with swap. What are you swapping?

You try to swap a string returned from a function with a string returned from a function. Both strings that you try to swap are unnamed temporary objects. Even if your swap would function, it would have no useful effect.


What are you sorting? An array of Invoices.
What should you swap in order to change the order of the Invoices in the array?
Invoices.


Is it your homework to write a selection sort? If not, then use std::sort.
"a) Use a Selection sort to sort the Invoice objects by PartDescription in ascending order."

Unfortunately it is! I was unsure about the swap part... I am trying to emulate a program I found in my book, because it is almost exactly the same as the assignment. How do I fix the swap?
If you do want to swap invoices[minIndex] with invoices[start], then do so:
std::swap( invoices[minIndex], invoices[start] );
Topic archived. No new replies allowed.