error LNK2019

Feb 24, 2017 at 7:06pm
I'm new to c++. This is my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "compute.h"

void compute( string inputfile, string outputfile )
{
  ifstream in( inputfile );
  ofstream out( outputfile );

  int answer;
  int number1, number2;

  out << setw(34) << "Operand1 " << "Operation " << "Operand2 " << "Answer " << endl;

  answer = add( number1,number2 );
  {
    answer = number1 + number2;
    out << setw(34) << number1 << "         " << "+         " << number2 << "         " << answer << "       " << endl;
  }

  answer = subtract( number1,number2 );
  {
    answer = number1 - number2;
    out << setw(34) << number1 << "         " << "-         " << number2 << "         " << answer << "       " << endl;
  }

  answer = multiply( number1,number2 );
  {
    answer = number1 * number2;
    out << setw(34) << number1 << "         " << "*         " << number2 << "         " << answer << "       " << endl;
  }

  answer = divide( number1,number2 );
  {
    answer = number1 / number2;
    out << setw(34) << number1 << "         " << "/         " << number2 << "         " << answer << "       " << endl;
  }

  answer = remainder( number1,number2 );
  {
    answer = number1 % number2;
    out << setw(34) << number1 << "         " << "%         " << number2 << "         " << answer << "       " << endl;
  }
}

and this is the header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;

using std::string;
using std::ifstream;
using std::ofstream;
using std::setprecision;
using std::fixed;
using std::showpoint;
using std::setw;

void compute( string inputfile, string outputfile );

int add( int number1, int number2 );
int subtract( int number1, int number2 );
int multiply( int number1, int number2 );
int divide( int number1, int number2 );
int remainder( int number1, int number2 );


I keep getting the error,
compute.obj : error LNK2019: unresolved external symbol "int __cdecl add(int,int)" (?add@@YAHHH@Z) referenced in function "void __cdecl compute(class std::basic_string<char,struct std::char_traits<char>,class s
td::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?compute@@YAXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z) for all 5 functions.
What does it mean and How do I fix it?
Thanks.
Last edited on Feb 24, 2017 at 7:07pm
Feb 24, 2017 at 9:01pm
I think there are two possible answers:

1) in the definition of the function
1
2
3
4
5
6
void compute (.., ..)
{

// Code surce

}

line 9: you have not initialized int number1 and number2, then the line 13 does not reach any value.

otherwise, from what you've posted, it may be that you have never defined the function
 
int add (int number1, int number2);

on the header file

Also I do not understand what it is:

1
2
3
4
answer = ...............
{

}

in your code, function "compute (,)", the "answer" variable assumes all values obtained by function calls placed in the code lines 13, 19, 25, 31, 37.

you wanted to use an if / else statement?

however, if I understood your problem, you have not defined any of the functions provided in the header file

int add (int number1, int number2);
int subtract (int number1, int number2);
int multiply (int number1, int number2);
int divide (int number1, int number2);
int remainder (int number1, int number2);


I suggest you read this: http://www.cplusplus.com/doc/tutorial/functions/
Last edited on Feb 24, 2017 at 9:15pm
Feb 24, 2017 at 9:05pm
It's possible that you are a victim of the copy-and-paste bug :-)
What happens if you remove your semicolons from rows 13, 19, 25, 31 and 37?
Feb 24, 2017 at 9:27pm
Enoizat:


What happens if you remove your semicolons from rows 13, 19, 25, 31 and 37?


I think it does not work anyway
Feb 24, 2017 at 10:11pm
1
2
3
4
5
6
 answer = add( number1,number2 ); // use of a declared, but not defined function 

  { // simple compound-statement introduces a nested lexical scope
    answer = number1 + number2;
    out << setw(34) << number1 << "         " << "+         " << number2 << "         " << answer << "       " << endl;
  }


This syntax is not a function definition. Instead, at namespace scope (outside the definition of compute), define add according to the tutorial @ar2007 linked to you.

Are you coming from a functional-styled language, perhaps? C++ does not support nested named functions, but rather function objects, and a different syntax for creating nameless ones (closures).
Feb 24, 2017 at 11:16pm
I fixed most of it but every time I try to output, the program only outputs add and ignores the other function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "compute.h"

int add( int number1, int number2 )
{
  int answer;
  answer = number1 + number2;
  return (answer);
}
int subtract( int number1, int number2 )
{
  int answer;
  answer = number1 - number2;
  return (answer);
}
int multiply( int number1, int number2 )
{
  int answer;
  answer = number1 * number2;
  return (answer);
}
int divide( int number1, int number2 )
{
  int answer;
  answer = number1 / number2;
  return (answer);
}
int remainder( int number1, int number2 )
{
  int answer;
  answer = number1 % number2;
  return (answer);
}

void compute( string inputfile, string outputfile )
{
  ifstream in( inputfile );
  ofstream out( outputfile );

  string oper;
  int answer;
  int number1, number2;

  out << "Operand1 " << "Operation " << "Operand2 " << "Answer " << endl;

  in >> oper;
  in >> number1;
  in >> number2;

  while ( !in.fail() )
  {
    if ( oper == "add" )
    {
      answer = add( number1,number2 );
      out << number1 << "        " << "+         " << number2 << "         " << answer << "       " << endl;
    }
    else if ( oper == "subtract" )
    {
      answer = subtract( number1,number2 );
      out << number1 << "        " << "-         " << number2 << "         " << answer << "       " << endl;
    }
    else if ( oper == "multiply" )
    {
      answer = multiply( number1,number2 );
      out << number1 << "        " << "+         " << number2 << "         " << answer << "       " << endl;
    }
    else if ( oper == "divide" )
    {
      answer = divide( number1,number2 );
      out << number1 << "        " << "+         " << number2 << "         " << answer << "       " << endl;
    }
    else if ( oper == "remainder" )
    {
      answer = remainder( number1,number2 );
      out << number1 << "        " << "+         " << number2 << "         " << answer << "       " << endl;
    }

    in >> answer;
    in >> oper;
    in >> number1;
    in >> number2;
  }
}
Feb 24, 2017 at 11:22pm
ok nevermind. I'm dumb.
Feb 25, 2017 at 3:06pm
hi.
can i see your main function?

Furthermore, also to display the value of the variable 'answer':
additing at line 76

 
std::cout << answer << '\n' ;
Last edited on Feb 25, 2017 at 3:14pm
Topic archived. No new replies allowed.