-2

I am getting multiple messages referring to

LNK2019: unresolved external symbol "int_cdecl findLowest(int,int)"

referenced in function_main . Whenever I try to compile my program 4 of these messages pop op. I have no idea on how to fix this otherwise I wouldn't be asking for help.

#include <iostream>
using namespace std;
// This program calculates the average of the inputed temperatures and finds the highest and lowest
// 
int main()
{
    int numOfTemp;
    int temp[50];
    int pos;

    double findAverage(int, int);
    int findLowest(int, int);
    int findHighest(int, int);

    cout << "Please input the number of temperatures to be read (no more than 50)" << endl;
    cin >> numOfTemp;

    for (pos = 1; pos <= numOfTemp; pos++)
    {
        cout << "Input temperature " << pos << ":" << endl;
        cin >> temp[pos];
    }

    cout << "The average temperature is " << findAverage(temp[pos], numOfTemp) << endl;
    cout << "The lowest temperature is " << findLowest(temp[pos], numOfTemp) << endl;
    cout << "The highest temperature is " << findHighest(temp[pos], numOfTemp) << endl;//calls function   
}

double findAverage(int table[], int num)
{
    for (int i = 0; i < num; i++)
    {
        int sum = 0;
        sum += table[i];

        return (sum / num); // calculates the average
    }    
}

int findLowest(int table[], int num)
{
    float lowest;    
    lowest = table[0]; // make first element the lowest price 

    for (int count = 0; count < num; count++)
        if (lowest > table[count])
            lowest = table[count];
        return lowest;
}

// This function returns the highest price in the array 
int findHighest(int table[], int num)
{
    float highest;    
    highest = table[0]; // make first element the highest price 

    for (int count = 0; count < num; count++)
        if (highest < table[count])
            highest = table[count];    
    return highest;
}
demonplus
  • 5,613
  • 12
  • 49
  • 68

1 Answers1

1

In C++, functions need to be declared before they are used. You can either place the function bodies for findAverage, findLowest and findHighest above main or use forward declarations.

EDIT: Make sure you are correctly declaring your function types! Like my comment says, you declare and attempt to call

double findAverage(int, int)

but only define

double findAverage(int[], int)

which will cause the linking stage to fail, because it cannot find your definition for the former.

Aiden Deom
  • 916
  • 5
  • 11
  • These functions actually are d eclared, but inside of `main()`s body. – πάντα ῥεῖ Oct 06 '15 at 16:58
  • I put the function bodies before the main and I am still getting the same messages. What is forward declaration? Is it the same as prototypes? – Jose Velazquez Oct 06 '15 at 17:03
  • 1
    @πάνταῥεῖ Ahh I see that now. Also, the functions he forward declared are not the same as the one's he defined. Different argument types. – Aiden Deom Oct 06 '15 at 17:14
  • Your last part is most probably solving the problem, you should edit your answer, to point this out. – πάντα ῥεῖ Oct 06 '15 at 17:18
  • @JoseVelazquez Forward declaring a function is declaring the function (it's signature) before it is defined. This allows you to compile. However, the linker error you are getting is becuase you are using functions that have not been defined. You have defined functions like `double findAverage(int[], int)` but declared and tried to use functions `double findAverage(int,int)`. See the difference? Since you are trying to use functions that have no definition, the linker will give you an error saying it cannot find the definition of the function. – Aiden Deom Oct 06 '15 at 17:19
  • @AidenDeom is the difference that one has (int[],int ) and the other is just (int,int)? – Jose Velazquez Oct 06 '15 at 17:25
  • @JoseVelazquez Exactly. One function declaration takes an array of `int`s and an `int`, while the other takes two `int` parameters. – Aiden Deom Oct 06 '15 at 17:29