-1

i am trying to understand the concepts of c++ as i am not very advanced in it. Although i have a descent knowledge , i have absolutely no idea about whats going on here!!!

I am making a program in which I simply have a template_class with a pointer to function which returns the value of function add. I am using Visual c++ and for some reason it returns the following error in main.cpp :-

Error C3867 'Template_class::add': non-standard syntax; use '&' to create a pointer to member`

main.cpp

#include<iostream>
#include<string>
#include"Template_class.h"

using namespace std;

int main()
{
    Template_class <int> t;

    t.retrunOperation(4, 5, t.add);
    cin.get();
}

Template_class.h

#include<iostream>

template<class T>
class Template_class
{
public:
    Template_class()
    {

    }
    ~Template_class()
    {

    }

    T add(T var1 , T var2)
    {
        return var1 + var2;
    }

    void  retrunOperation(T var1 , T var2 , T (*function)(T , T))
    {
        std::cout << (*function)(var1, var2);
    }
};

This may be a invalid question(about which i have no idea) , but as i said i am not an advanced programmer , so please suggest me some solutions

Jonas
  • 6,915
  • 8
  • 35
  • 53
mitesh
  • 235
  • 3
  • 11
  • Isn't the error telling you **exactly** how to fix it? – SergeyA Jan 19 '17 at 16:46
  • Did you try to do what error suggests? – Revolver_Ocelot Jan 19 '17 at 16:46
  • 3
    No, the error is not saying how to fix this. That would just lead to a different error. –  Jan 19 '17 at 16:47
  • no its not .... i wrote the whole thing in the body ..... its telling me to use a reference to create pointer to a function ... what does that mean?? – mitesh Jan 19 '17 at 16:49
  • related/dupe: http://stackoverflow.com/questions/8045322/c-function-callbacks-cannot-convert-from-a-member-function-to-a-function-sign – NathanOliver Jan 19 '17 at 16:52
  • 1
    It *does* say that to get a pointer to member function, you should use `&Template_class::add`. Then you have to look into the type of the function parameter, to make that match. – Bo Persson Jan 19 '17 at 16:53

3 Answers3

0
void  retrunOperation(T var1 , T var2 , T (*function)(T , T))

The function argument expects a function pointer.

A (regular) function pointer cannot point to a non-static member function. Template_class::add is a non-static member function. Therefore it cannot be pointed by a function pointer.

Most trivial solution is to declare Template_class::add to be a static member function, using the static keyword. Static member functions can be pointed by a function pointer.


A name of a non member function will implicitly decay into a pointer to the function, without using the addressof operator (&). Non static member functions can be pointed by member function pointers, which are not convertible to (regular) function pointers. However, name of a non static member function does not implicitly decay into a member function pointer. That is what the error message is telling you. If you fixed that, by adding the addressof operator, then you would have been given another error about lack of fitting overload.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • thanks user2079393 !!! that did solve the problem !! But can you please tell me some more about static functions and pointer related stuff to this !! because that would really help me! – mitesh Jan 19 '17 at 16:56
  • @mitesh this should help: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – eerorika Jan 19 '17 at 16:58
0

The compiler error is telling you that the right way to pass a member function is

t.retrunOperation(4, 5, &Template_class<int>::add);

However, when you fix that, you'll find that retrunOperation() wants a pointer to function rather than a pointer to member function. You'll then need to make add static:

static T add(T var1 , T var2)
{
    return var1 + var2;
}

or (less likely) declare retrunOperation() as taking a pointer to member function:

void  retrunOperation(T var1 , T var2 , T (Template_class<int>::*function)(T , T))

and implement it appropriately.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
  • yes i made add static and it worked !! thanks !! i greatly appreciate the support . But i want to know some more about static . Can you tell me? – mitesh Jan 19 '17 at 16:59
  • Pointer to member is different from pointer to member function :) – eerorika Jan 19 '17 at 16:59
  • 1
    @mitesh *Any* [decent book on C++](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list?s=1|32.8400) coveres the difference between members and static members (both data and functions) at length. If you don't have at least one, you should rectify that. – WhozCraig Jan 19 '17 at 17:05
  • no i have one ... but as i am a student , i am not getting a lot of time to read it thoroughly. – mitesh Jan 19 '17 at 17:13
0

Non-static member functions have the signature:

Ret Class::name(Type, Type2...);

A pointer to a member function then would look like:

Ret Class::* name(Type, Type...);

So when you say:

t.retrunOperation(4, 5, t.add);

you are trying to give

int(*function)(int, int)

where

int(Template_class<int>::*function)(int , int)

is expected.

Applying the above, the solution to your specific problem would look like so:

template<class T>
class Template_class
{
public:
    T add(T var1 , T var2) { return var1 + var2; }

    void  retrunOperation(T var1 , T var2 , T(Template_class<T>::*function)(T , T)) {}
};

and you can call it like this:

t.retrunOperation(4, 5, &Template_class<int>::add);
DeiDei
  • 10,205
  • 6
  • 55
  • 80