我收到多个关于LNK2019的消息:未解析的外部符号

I am getting multiple messages referring to LNK2019: unresolved external symbol

本文关键字:消息 符号 外部 LNK2019      更新时间:2023-10-16

我收到多个关于

的消息

LNK2019:无法解析的外部符号"int_cdecl findlow (int,int)"

在function_main中被引用。每当我试图编译我的程序时,这些消息中就有4个弹出。我不知道如何解决这个问题,否则我就不会寻求帮助了。

#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;
}

在c++中,函数需要在使用之前声明。您可以将findAverage, findLowestfindHighest的函数体放在main之上,或者使用前向声明。

EDIT:确保正确声明函数类型!就像我的评论说的,你声明并试图调用

double findAverage(int, int)

但只定义

double findAverage(int[], int)

将导致链接阶段失败,因为它找不到前者的定义