Search code examples
c++linker-errorsfunction-declaration

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.


Solution

  • If that's all of your code, then you probably get the linking errors because you're missing a main function. I get those exact two linking errors if I omit main on my VS project. Add this to your code:

    int main() {
        question();
    }
    

    Also, you're not calling your functions, you're just declaring them:

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

    Remove those int to call the functions. You'll have to put int question() below those two other functions or it won't find them, unless you declare them beforehand, like this:

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