[英]Getting error : C3867 'Template_class<int>::add': non-standard syntax; when using pointers to functions with templates
i am trying to understand the concepts of c++ as i am not very advanced in it.我正在尝试理解 C++ 的概念,因为我在这方面不是很先进。 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.我正在制作一个程序,其中我只有一个模板类,它带有一个指向函数的指针,该函数返回函数 add 的值。 I am using Visual c++ and for some reason it returns the following error in main.cpp :-我正在使用 Visual c++,由于某种原因,它在 main.cpp 中返回以下错误:-
Error C3867 'Template_class::add': non-standard syntax;错误 C3867 'Template_class::add':非标准语法; 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模板类.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这可能是一个无效的问题(我不知道),但正如我所说我不是一个高级程序员,所以请给我一些解决方案
void retrunOperation(T var1 , T var2 , T (*function)(T , T))
The function
argument expects a function pointer. function
参数需要一个函数指针。
A (regular) function pointer cannot point to a non-static member function. (常规)函数指针不能指向非静态成员函数。 Template_class::add
is a non-static member function. Template_class::add
是一个非静态成员函数。 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
关键字Template_class::add
声明为静态成员函数。 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 ( &
).非成员函数的名称将隐式衰减为指向该函数的指针,而不使用 addressof 运算符 ( &
)。 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.如果您通过添加 addressof 运算符修复了该问题,那么您将收到另一个关于缺少拟合重载的错误。
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 .然而,当你解决这个问题时,你会发现retrunOperation()
想要一个指向function的指针而不是一个指向成员 function的指针。 You'll then need to make add
static:然后你需要add
静态:
static T add(T var1 , T var2)
{
return var1 + var2;
}
or (less likely) declare retrunOperation()
as taking a pointer to member function:或(不太可能)将retrunOperation()
) 声明为指向成员函数的指针:
void retrunOperation(T var1 , T var2 , T (Template_class<int>::*function)(T , T))
and implement it appropriately.并妥善实施。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.