Linker Error

Error with code in DevCPP

[Linker error] undefined reference to `Test::testfunc()'

Error with code in Microsoft Visual Studeio

main.obj : error LNK2019: unresolved external symbol "public: void __thiscall Test::testfunc(void)" (?testfunc@Test@@QAEXXZ) referenced in function _main
1>c:\users\ryan\documents\visual studio 2013\Projects\ConsoleApplication4\Debug\ConsoleApplication4.exe : fatal error LNK1120: 1 unresolved externals




main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <cstdlib>
#include <iostream>
#include "test.h"


using namespace std;



int main(int argc, char *argv[])
{
	Test test;
	test.testfunc();

	system("PAUSE");
	return EXIT_SUCCESS;
}


test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13

#include <iostream>
#include "test.h" // class's header file

using namespace std;




void testfunc() {
	cout << "Hello World." << endl;
}


test.h header file
1
2
3
4
5
6
7
8
9
10
#ifndef TEST_H
#define TEST_H

class Test {
public:
	void testfunc();

};

#endif // TEST_H 
In test.cpp you have declared a function that is not part of any class. If you want to define the testfunc() that is in Test you need to write Test:: in front of the function name.
Last edited on
Topic archived. No new replies allowed.