Undefined reference to 'function()'

Hi,

I am beginner in C++ and am using Code::blocks. I have surfed the web and wanted to test a simple code. The code consists of a main.cpp, PrintTest.cpp, and PrintTest.h. When I use the function within main file (main.cpp), there is no problem. But, the following error pops out when the function is in a separate file. Any help will be appreciated.

Error:
undefined reference to 'PrintTest(int, int)'

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
// main.cpp code
#include <iostream>
#include <cstdlib>
#include "PrintTest.h"

using namespace std;

int PrintTest(int,int);

int main()
{

    int d;
    d = PrintTest(3,4);
    cout << "The result is " << d;
    return 0;

}

// PrintTest.cpp code
#include <iostream>
#include <cstdlib>
#include "PrintTest.h"

int PrintTest (int a, int b)
{
    int c;
    c = a + b;
    return c;

}

// PrintTest.h code
#ifndef PRINTTEST_H_INCLUDED
#define PRINTTEST_H_INCLUDED

#include <iostream>
#include <cstring>
using namespace std;

int PrintTest(int, int);

#endif // PRINTTEST_H_INCLUDED

closed account (E0p9LyTq)
You are declaring int PrintTest(int, int) twice, once in your header file (line 41) and then again in your main unit (line 8).

Delete/comment out either one. Personally I would do the prototype at line 8, in your main unit. Keep the function prototype in your header file.
There is no problem with multiple declaration.


Your didn't configure your project properly
http://www.cplusplus.com/forum/general/113904/#msg622055
Topic archived. No new replies allowed.