I'm trying to to write a code that will call a function that receive a pointer to other function within the same class, and call get_num method from main. but when doing include to TestClass.h from main, I'm getting linkage errors
class TestClass{
public:
void get_num(int num);
void foo(int num, void(TestClass::*function)(int));
void boo(int num);
};
void TestClass::boo(int num)
{
std::cout << "number: " << num << std::endl;
}
void TestClass::foo(int num, void(TestClass::*function)(int))
{
(this->*function)(num);
}
void TestClass::get_num(int num)
{
foo(num, &TestClass::boo);
}
Following is the main code:
#include "TestClass.h"
int main()
{
TestClass tc1;
tc1.get_num(5);
system("pause");
return 1;
}
The following errors appears:
1>main.cpp
1>TestClass.obj : error LNK2005: "public: void __thiscall TestClass::boo(int)" (?boo@TestClass@@QAEXH@Z) already defined in main.obj
1>TestClass.obj : error LNK2005: "public: void __thiscall TestClass::foo(int,void (__thiscall TestClass::*)(int))" (?foo@TestClass@@QAEXHP81@AEXH@Z@Z) already defined in main.obj
1>TestClass.obj : error LNK2005: "public: void __thiscall TestClass::get_num(int)" (?get_num@TestClass@@QAEXH@Z) already defined in main.obj
1>c:\Proj4.exe : fatal error LNK1169: one or more multiply defined symbols found