为什么Visual Studio 2019不编译我的代码?

时间:2019-08-28 07:03:37

标签: c++ linker-errors function-declaration

我一直在尝试使此代码正常工作。我主要是自己和Google编写此代码的。我对此非常初级,所以我不知道如何解决此问题。

我尝试剪切代码,将其转换为c#,(当然要修改)新文件和其他编译器。

#include <iostream>
using namespace std;

//suurin_luku.cpp 

int question() {
    double answers;
    cout << "Do you want the biggest number, or smallest number?\n";
    cout << "0 for biggers, 1 for smaller.\n";
    cin >> answers;

    if (answers == 0) {
        int biggest();
    }
    if (answers == 1) {
        int smallest();

    }
    return 0;
}






int biggest()
{
    float input1, input2, input3;
    cout << "Please insert three numbers.\n";
    cin >> input1 >> input2 >> input3;
    if (input1 >= input2 && input1 >= input3)
    {
        cout << "The largest number is: " << input1;
    }
    if (input2 >= input1 && input2 >= input3)
    {
        cout << "The largest number is: " << input2;
    }
    if (input3 >= input1 && input3 >= input2) {
        cout << "The largest number is: " << input3;
    }
    return 0;
}

int smallest()
{
    float input11, input22, input33;
    cout << "Insert three numbers.";
    cin >> input11 >> input22 >> input33;
    if (input11 <= input22 && input11 <= input33)
    {
        cout << "The smallest number is: " << input11;
    }
    if (input22 <= input11 && input22 <= input33)
    {
        cout << "The smallest number is: " << input22;
    }
    if (input33 <= input11 && input33 <= input22) {
        cout << "The smallest number is: " << input33;
    }
    return 0;
    }

当用户输入0时,它将显示最大的输入数字。 用户输入1时,显示输入的最小数字。 错误代码为LNK1120和LNK2019。

1 个答案:

答案 0 :(得分:1)

如果这就是您的全部代码,则可能会由于缺少main函数而导致链接错误。如果我在VS项目中省略main,则会得到这两个链接错误。将此添加到您的代码中:

int main() {
    question();
}

此外,您不是在调用函数,而是在声明它们:

if (answers == 0) {
    int biggest();
}
if (answers == 1) {
    int smallest();
}

删除那些int以调用函数。您必须将int question()放在其他两个函数的下面,否则除非您事先声明它们,否则它将找不到它们,

int biggest(); // declaring them so question() knows their signature
int smallest();
int question() { ... }; // question() calls biggest() and smallest()
int biggest() { ... }; // actual implementation of those functions
int smallest() { ... };
int main { ... }