-1

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
ol202020
  • 95
  • 6

1 Answers1

2

Pointer to a method is not the same as a pointer to a simple function. If you only want to be able to accept a pointer to the method of the same class you can rewrite your method like this:

void TestClass::foo(int num, void(TestClass::*function)(int))
{
    (this->*function)(num);
}

void TestClass::get_num(int num)
{
    foo(num, &TestClass::boo);
}

This allows foo() to accept a pointer to any method in TestClass but not to any method of any other class nor to a simple function. It also calls the received method on the same object (note: this in this->*function).

r3mus n0x
  • 5,954
  • 1
  • 13
  • 34
  • Thanks, now I'm not getting an error when compiling, but if I'm doing " #include "TestClass.h" from main.cpp I'm getting linkage errors: 1>TestClass.obj : error LNK2005: "public: void __thiscall TestClass::boo(int)" (?boo@TestClass@@QAEXH@Z) already defined in main.obj Proj4.exe : fatal error LNK1169: one or more multiply defined symbols found – ol202020 Jul 16 '18 at 16:39
  • @user7377723, please update your question specifying all files that you are using and their content. Your problem is probably that you have `foo()` function's body in `TestClass.h`. You should move all functions' definitions (bodies) to `TestClass.cpp`. – r3mus n0x Jul 16 '18 at 16:42
  • @user7377723, I'm afraid this has turned in a completely different question now. But as I said the problem is that you probably have both `main.cpp` and `TestClass.cpp` that include `TestClass.h` which contains function bodies. Either move all `TestClass'` function bodies to `TestClass.cpp` or remove `TestClass.cpp` entirely. – r3mus n0x Jul 16 '18 at 16:59
  • Thanks you very much r3mus, it is working now... And I also understand your explanation, I really appreciate you assistance! – ol202020 Jul 16 '18 at 19:44