+2 instances overload

Im having trouble solving this error and does not know what does it mean. I tried this kind of code before but it does not have this error. When calling the sortArray function it have 2 instances overload

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
 #include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<cctype>
#include<fstream>
#include<cstdlib>

using namespace std;

void sortArray(NUMBER n[],int size);

typedef struct
{
	int decimal;
	char binary[50];
	char octal[50];
	char hexadecimal[50];
}NUMBER;

int main(void)
{
	NUMBER numbers[100]; // maximum of 100 numbers
	int count = 0;
	ifstream inFile;

	inFile.open("numbers.txt");
	if (inFile.fail())
	{
		cout << "Error opening file !\n";
		exit(1);
	}
	else
	{
		inFile >> numbers[count].decimal;

		while (inFile)
		{
			count++;
			inFile >> numbers[count].decimal;
		}
	}

	inFile.close();

	sortArray(numbers, count);

	for (int i = 0; i < count; i++)
	{
		cout << numbers[i].decimal << endl;
	}
}

void sortArray(NUMBER n[],int size)
{
	int temp; // temporary variable to compare data

	for (int i = 0; i < size; i++)
	{
		for (int j = i + 1; j < size; j++)
		{
			if (n[i].decimal > n[j].decimal)
			{
				temp = n[i].decimal;
				n[i].decimal = n[j].decimal;
				n[j].decimal = temp;
			}
		}
	}
}
Last edited on
The compiler will only scan from top to bottom, so it must know what a NUMBER is before parsing the function declaration.

This is what the compiler sees by the time it parses line 11:
1
2
3
4
5
6
7
8
9
10
11
 #include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<cctype>
#include<fstream>
#include<cstdlib>

using namespace std;

void sortArray(NUMBER n[],int size);

At this point, the compiler does not know what a NUMBER is, but you use it for your sortArray function.

Solution 1: Forward declare struct (and don't do the "typedef struct" thing, that is a C relic).
1
2
3
4
5
6
7
8
9
10
11
struct NUMBER;

void sortArray(NUMBER n[],int size);

struct NUMBER
{
	int decimal;
	char binary[50];
	char octal[50];
	char hexadecimal[50];
};


Solution 2: Define the struct before the function declaration.
1
2
3
4
5
6
7
8
9
struct NUMBER
{
	int decimal;
	char binary[50];
	char octal[50];
	char hexadecimal[50];
};

void sortArray(NUMBER n[],int size);


PS: In general, define variables in the smallest scope possible. For example, your 'temp' variable is overwritten and only used in the narrowest scope within your if-statement, so you can declare it within there instead of on line 56.
1
2
3
int temp =  n[i].decimal;
n[i].decimal = n[j].decimal;
n[j].decimal = temp;

This also help you get into the habit of declaring + initializing things on the same line when possible.

PPS: You can use std::swap instead of your own swap code.
1
2
3
4
5
6
7
#include <algorithm>

// ...
	if (n[i].decimal > n[j].decimal)
	{
		std::swap(n[i].decimal, n[j].decimal);
	}

Last edited on
Hello NiceS,

I see that you have marked this finished, but I wanted to point something out.

1
2
3
4
5
6
if (n[i].decimal > n[j].decimal)
{
    temp = n[i].decimal;
    n[i].decimal = n[j].decimal;
    n[j].decimal = temp;
}

The if statement is fine, but the swap is swapping 1 single variable of the struct and not the rest. What you need to do is swap the entire struct element.

Andy
Also, the input for the numbers can be simplified by making the extraction the while condition. In addition, std::sort() can be used instead of writing your own (unless this is a requirement).

Consider:

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
#include <iostream>
#include <fstream>
#include <algorithm>

struct Number
{
	int decimal {};
	char binary[50] {};
	char octal[50] {};
	char hexadecimal[50] {};
};

constexpr size_t NONUMS {100};

int main() {
	std::ifstream inFile("numbers.txt");

	if (!inFile)
		return (std::cout << "Error opening file\n"), 1;

	Number numbers[NONUMS] {}; // maximum of 100 numbers
	size_t count {};

	while ((count < NONUMS) && (inFile >> numbers[count++].decimal));

	std::sort(numbers, numbers + count, [](const auto& lh, const auto& rh) {return lh.decimal < rh.decimal; });

	for (const auto& n : numbers)
		std::cout << n.decimal << '\n';
}


Last edited on
If one goes to <algorithm>, then one could go to std::sort too:
1
2
3
4
5
6
7
8
// Sort numbers by member decimal:
// Own sort:
sortArray( numbers, count );
// Library sort:
std::sort( numbers, numbers+count,
           [](const auto& lhs, const auto& rhs) {
             return lhs.decimal < rhs.decimal;
           } );

The [](const auto& lhs, const auto& rhs){return lhs.decimal < rhs.decimal;}
is a lambda closure, an unnamed function object.
Lambdas were added in C++11. Support for C++11 has to be explicitly enabled on older compilers.


Note, int main(void) is C. It is accepted in C++, but int main() is the preferred way to say "we take 0 parameters".
Topic archived. No new replies allowed.