Error Message: Unresolved Externals

Hello. I am receiving two error messages that both include "unresolved externals." My goal is basically to use functions to read data from a file, sum it, then average it. Does anyone see the problem? Here is my code:

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

void ReadAndSum(ifstream&, int&, float&);
// Reads, counts, and averages values on a file.

int main ()
{
ifstream dataFile;
int numberOfValues;
float average;
cout << fixed << showpoint;
dataFile.open("Averages.dat");

ReadAndSum(dataFile, numberOfValues, average);

cout << "The average of " << numberOfValues
<< " values is " << average << endl;
return 0;
}
//*************************************************

void ReadAndSum (int data, int number, float avg)
// Function Definition
{
float sum;
sum = 0;

if (! data)
{
cout << "File opening error. " <<
"Program terminated." << endl;
}

while (data)
{
cin >> data;
sum += data;
number += 1;
}
avg = sum/number;
}
Sorry, the error codes are LNK2019 and LNK1120. I researched them but it still makes no sense to me.
The problem is that the prototype void ReadAndSum(ifstream&, int&, float&); differs from the actual implementation void ReadAndSum (int data, int number, float avg).
So the linker is looking for that function with '&' but there's none
So, I need to change the argument? Sorry, I'm not quite following.
The linker is giving you an error because it can't find the body for this function:

void ReadAndSum(ifstream&, int&, float&);

You do not have a body for this function anywhere in your program. To solve this error, this function must have a body.

Note that you have a body for a similar function with the same name here:

1
2
3
4
void ReadAndSum (int data, int number, float avg)
{
  //...
}


However since the parameter list is different, this is a different function.




Basically -- the parameter list of your function prototype and function body must match exactly.
Last edited on
Thank you very much. I think I am beginning to understand. So, the error is because I am not including the ampersand in the function definition? I just added them and I am still receiving the same error.

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
// Program Averages averages the values on file dataFile.
#include <iostream>
#include <fstream>
using namespace std;

void  ReadAndSum(ifstream&, int&, float&);
// Reads, counts, and averages values on a file.

int main ()
{
  ifstream  dataFile;
  int  numberOfValues;
  float  average;
  cout << fixed << showpoint;
  dataFile.open("Averages.dat");
    
  ReadAndSum(dataFile, numberOfValues, average);

  cout << "The average of " << numberOfValues
       << " values is " << average  << endl;
  return 0;
}
//*************************************************

void  ReadAndSum (int& data, int& number, float& avg)
// Function Definition
{
	float sum;
	int x;
	sum = 0;
	x = 0;

	if (! data)
	{
		cout << "File opening error. " << 
			"Program terminated." << endl;
	}

	while (data)
	{
		data >> x;
		sum += x;
		number += 1;
	}
	avg = sum/number;
}
Ugh. Never mind. I figured it out. I feel like an idiot. Thanks for all your help!!!
Topic archived. No new replies allowed.