-1

I have a pretty syntax-correct and very simple app that takes two integers from user and do substraction and addition of them. But when i try to compile a program, i'm getting this error:

Could not find 'C:\Users\MyUsername\source\repos\SimpleCalculator\SimpleCalculator\Debug\SimpleCalculator.obj'. SimpleCalculator.exe was built with /DEBUG:FASTLINK which requires object files for debugging.

Here is my code:

#include "stdafx.h"
#include <iostream>
using namespace std;

void main()
{
int a, b;
cout << "Welcome." << endl;

cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ";
cin >> b;

cout << "A+B is: ";
cout << addition(a,b) << endl;

cout << "A-B is: ";
cout << substraction(a,b) << endl;

system("pause");

}

int addition(int a, int b) {
    return a + b;
}

int substraction(int a, int b) {
    return a - b;
}

This error only happens when i have functions. When my code is like:

cout << "A+B is: ";
cout << a+b << endl;

there is no errors.

Arnav Borborah
  • 10,781
  • 6
  • 36
  • 77

4 Answers4

4

You need to declare functions before you call them, so you basically have 2 options:

Declare them before main:

int addition(int a, int b);
int substraction(int a, int b);

or move the whole definition of them in front of the main.

463035818_is_not_a_number
  • 88,680
  • 9
  • 76
  • 150
1

Just declare the functions before you use them, like this:

int addition(int a, int b);
int substraction(int a, int b);

and you will be fine.

Or, move the definitions (implementations) of your functions, before main().


PS: What should main() return in C and C++? An int, not void.

gsamaras
  • 69,751
  • 39
  • 173
  • 279
0

The C++ compiler reads your code top to bottom. It is not aware of the existence of addition until it is declared - attempting to use it before a declaration is an error.

To fix your compiler error, move the definitions of addition and substraction above main.

Vittorio Romeo
  • 86,944
  • 30
  • 238
  • 393
0

In C++ (and C99, C11 but not older C standards) you must declare functions before you can use them. This should be the same as the definition but without the body ({}) like what you find in header files.

int addition(int a, int b);
int substraction(int a, int b);
void main()
{
    ...
Fire Lancer
  • 28,428
  • 27
  • 111
  • 175