首页 > 解决方案 > Why doesn't Visual Studio 2019 compile my code?

问题描述

I have been trying to get this code working. I have made this code mostly myself and google. I am very beginner in this, so I have no idea how to fix this.

I have tried cutting code, transforming it to c#,(modifying it of course) new file and different compiler.

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

When user inputs 0, it shows the largest inputted number. When user inputs 1, it shows the smallest inputted number. Error codes are LNK1120 and LNK2019.

标签: c++linker-errorsfunction-declaration

解决方案


如果这就是您的所有代码,那么您可能会收到链接错误,因为您缺少一个main函数。如果我main在我的 VS 项目中省略,我会得到这两个确切的链接错误。将此添加到您的代码中:

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 { ... }

推荐阅读