【问题标题】:I am getting multiple messages referring to LNK2019: unresolved external symbol [duplicate]我收到多条关于 LNK2019 的消息:未解析的外部符号 [重复]
【发布时间】:2015-10-06 16:48:42
【问题描述】:

我收到多条涉及

的消息

LNK2019:未解析的外部符号“int_cdecl findLowest(int,int)”

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

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

【问题讨论】:

  • 如果您想获得帮助,请发布确切的错误消息
  • 您是否尝试在 main() 之外声明您的函数?
  • @demonplus 我在里面编辑了消息
  • “为什么...”并不完全是重复的,但我认为该问题的标题包含您问题的答案。

标签: c++ arrays function input output


【解决方案1】:

在 C++ 中,函数需要在使用前声明。您可以将 findAveragefindLowestfindHighest 的函数体放在 main 上方,也可以使用前向声明。

编辑:确保您正确声明了您的函数类型!正如我的评论所说,您声明并尝试调用

double findAverage(int, int)

但只定义

double findAverage(int[], int)

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

【讨论】:

  • 这些函数实际上是被声明的,但在main()s 正文中。
  • 我将函数体放在 main 之前,我仍然收到相同的消息。什么是前向声明?和原型一样吗?
  • @πάνταῥεῖ 啊,我现在明白了。此外,他前向声明的函数与他定义的函数不同。不同的参数类型。
  • 你的最后一部分很可能是解决问题,你应该编辑你的答案,指出这一点。
  • @JoseVelazquez 转发声明函数是在定义函数之前声明函数(它的签名)。这允许您编译。但是,您得到的链接器错误是因为您正在使用尚未定义的函数。您已经定义了类似double findAverage(int[], int) 的函数,但声明并尝试使用函数double findAverage(int,int)。看到不同?由于您尝试使用没有定义的函数,链接器会给您一个错误,说它找不到函数的定义。
猜你喜欢
  • 2014-11-07
  • 2013-06-01
  • 1970-01-01
  • 2013-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
相关资源
最近更新 更多