Question:
C++ fatal error, help please?
Remzi
2010-04-19 14:40:55 UTC
Hi, someone on here just helped me fix up a little error, and now when I run it I'm getting a fatal error, help please.

Code:

#include "stdafx.h"
#include
using namespace std;
double calcAverage(double sum, double count)
{
if (count > 0) {
return sum / (count * 1.0);
}
throw("calcAverage: Division by zero exception!");
return sum;
}

float calcAverage(double,int);
int _tmain(int argc, _TCHAR* argv[])
{
cout<<"Input ten different floating numbers:";
double sum = 0;
double average = 0;
double numbers[25];
int count;

for (count = 1; count <=10; count ++)
{
cin>> numbers[count];
sum = sum + numbers[count];
}

average = calcAverage(sum,count);
cout<<"The average of the numbers in this array is " << average << ".";
return 0;
}
--------------------------

Output:

1>------ Build started: Project: FindAverage, Configuration: Debug Win32 ------
1>Compiling...
1>FindAverage.cpp
1>Compiling manifest to resources...
1>Microsoft (R) Windows (R) Resource Compiler Version 6.1.6723.1
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>Linking...
1>FindAverage.obj : error LNK2019: unresolved external symbol "float __cdecl calcAverage(double,int)" (?calcAverage@@YAMNH@Z) referenced in function _wmain
1>C:\Users\Amir\Documents\Visual Studio 2008\Projects\FindAverage\Debug\FindAverage.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://c:\Users\Amir\Documents\Visual Studio 2008\Projects\FindAverage\FindAverage\Debug\BuildLog.htm"
1>FindAverage - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Three answers:
The Phlebob
2010-04-19 14:50:27 UTC
Your definition of calcAverage() returns a double, but the declaration the _tmain() will use, right above it, returns a float. This is the one the compiler is using to generate the call for and that's what the linker is reporting as the unresolved external symbol.



And since calcAverage() is defined above all its callers and in the same file, the declaration is superfluous. You only need one if a) the definition follows at least one function that uses or or b) the definition is in a separate file so the callers can't determine its calling sequence (in which case it should be in a separate header file).



Hope that helps.
Jazka
2010-04-19 23:15:08 UTC
When you wrote your function definition, you declared calcAverage as a double, but in your function header, you declared it as a float.



This code works (tested in Dev C++)



#include

using namespace std;



double calcAverage(double,double);



double calcAverage(double sum, double count)

{

if (count > 0)

{

return sum / (count * 1.0);

}

throw("calcAverage: Division by zero exception!");

return sum;

}







int main()

{

cout << "Input ten different floating numbers:";

double sum = 0;

double average = 0;

double numbers[25];

int count;



for (count = 1; count <=10; count ++)

{

cin >> numbers[count];

sum = sum + numbers[count];

}



average = calcAverage(sum,count);



cout << "The average of the numbers in this array is " << average << ".";



system ("PAUSE");



return 0;

}
wing_2_fly
2010-04-19 14:53:42 UTC
double calcAverage(double sum, double count)

{

if (count > 0) {

return sum / (count * 1.0);

}

throw("calcAverage: Division by zero exception!");

return sum;

}



float calcAverage(double,int);



this is the problem... you have defined function before you've even put declaration...

second thing ...

In declaration is float calcAverage(double, int);

but in implementation you use

double calcAverage(double, double);



try removing line float calcAverage(double, int) and rerun app and see if it works...


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...