0

I am having problems using this declaration.

object1.function1<Object2, void (Object2::*)()>(object2, &Object2::function2)

This is what the compiler tells me.

error LNK2001: unresolved external symbol "public: void __cdecl Object1::function1<class Object2,void (__cdecl Object2::*)(void)>(class Object2 &,void (__cdecl Object2::*)(void))" (??$function1@VObject2@@P81@EAA_NXZ@Object1@@QEAA_NAEAVObject2@@P81@EAA_NXZ@Z)

Here the struct of the code:

class Object1{
public:
    template <typename O, typename F>
    void function1(O& object, F function){
        object.function();
    }
};

class Object2{
public:
    void function2(){
        std::cout << "Doing something..." << std::endl;
    };
};

class Object3 {
private:

    Object1 object1;
    Object2 object2;

    void function3() {
        object1.function1<Object2, void (Object2::*)()>(object2, &Object2::function2);
    };
};

I can not see the error. Someone could help me?

vicalomen
  • 1
  • 2
  • 1
    `std::invoke(function, &object);` in `function1` template. – rafix07 Mar 19 '21 at 12:32
  • 1
    @rafix07, `&` can be omitted. – Evg Mar 19 '21 at 12:35
  • 3
    Take the above code, make a project with *only it* in it (ok you can add a main) in one cpp file, compile, and provide *all compiler output* verbatim. Include what compiler and the command line if you can. Because my "in head" compiler gets a different error message from the code you posted, so I think your code is not a [mcve] of your actual problem. I think you simplified and manually edited the error message, and in doing so made your example not reproduce your problem. But I could be wrong, my "in head" compiler lacks iso certification. – Yakk - Adam Nevraumont Mar 19 '21 at 12:36
  • If you fix the function names, it compiles fine ([https://www.ideone.com/u05Qd5](https://www.ideone.com/u05Qd5)). Take Yakk's advice. – zdf Mar 19 '21 at 12:52
  • Note that you don't have to specify template parameter, as they are deducible, just do `object1.function1(object2, &Object2::function2);` – Jarod42 Mar 19 '21 at 13:05
  • @zdf: I suspect OP want to use the member function pointer, not expect a method called `function` in `O`. – Jarod42 Mar 19 '21 at 13:07
  • It is a simplification of the original code because it is to complex to understand. When I simplify it, it works, but in the original base code it does not. – vicalomen Mar 20 '21 at 20:13

1 Answers1

1

In

class Object1{
public:
    template <typename O, typename F>
    void function1(O& object, F function){
        object.function();
    }
};

object.function(); doesn't use F function.

Syntax for member function pointer would be:

(object.*function)();

std::invoke is superior, as it allows more than just member function

std::invoke(function, object);

Demo

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • What @Jarod42 is right, but the problem still exist. – vicalomen Mar 20 '21 at 20:09
  • @vicalomen: Demo added. link error means missing definition, that you can happen if you forget to build all files together, implment template in cpp files, ... – Jarod42 Mar 22 '21 at 08:16
  • Finally I found the error. It appears when you declare the function template in a .cpp class file. I do not why. Do you @Jarod42 know the reason? – vicalomen Apr 08 '21 at 22:22
  • @vicalomen: see [why-can-templates-only-be-implemented-in-the-header-file](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Jarod42 Apr 09 '21 at 07:48