类内的函数指针(表观调用的括号前面的表达式必须具有(指向)函数类型)
我想根据输入使用不同的函数来计算输出。 但它说:(表观调用的括号前面的表达式必须具有(指向)函数类型)
int (TestClass::* Gate_Func)(vector); <<==这是我发起的功能。
然后这里: Gate_Func = &TestClass::AND; 我可以用 Gate_Func 引用 AND 函数,
但为什么 输出 = Gate_Func(InputArr); 这个有一个错误: C++ 显然调用的表达式前面的括号必须具有(指向)函数类型
#include <iostream>
#include <vector>
using namespace std;
class TestClass {
public:
int AND(vector<int> inputarr) {
return (inputarr[0] == 1 && inputarr[1] == 1);
}
int OR(vector<int> inputarr) {
return (inputarr[0] == 1 || inputarr[1] == 1);
}
int NOT(vector<int> inputarr) {
if (inputarr[0] != 0) return 0;
else return 1;
}
int (TestClass::* Gate_Func)(vector<int>);
TestClass(int ChooseFunction, vector<int> inputarr) {
InputArr = inputarr;
switch (ChooseFunction)
{
case 1:
Gate_Func = &TestClass::AND;
break;
case 2:
Gate_Func = &TestClass::OR;
break;
case 3:
Gate_Func = &TestClass::NOT;
break;
default:
break;
}
}
void calculation() {
output = Gate_Func(InputArr); //C++ expression preceding parentheses of apparent call must have (pointer-to-) function type
}
vector<int> InputArr;
int output = 2;
void printOutput() { cout << output << endl; }
};
int main() {
vector<int> input = { 1, 1 };
TestClass obj(1, input);
obj.printOutput();
}
哦!我明白了,谢谢你们,所以当我使用“this”时,它基本上就像调用类的对象,我必须从该对象调用函数!
这是代码:
#include <iostream>
#include <vector>
using namespace std;
class TestClass {
public:
int AND(vector<int> inputarr) {
return (inputarr[0] == 1 && inputarr[1] == 1);
}
int OR(vector<int> inputarr) {
return (inputarr[0] == 1 || inputarr[1] == 1);
}
int NOT(vector<int> inputarr) {
if (inputarr[0] != 0) return 0;
else return 1;
}
int (TestClass::* Gate_Func)(vector<int>);
TestClass(int ChooseFunction, vector<int> inputarr) {
InputArr = inputarr;
switch (ChooseFunction)
{
case 1:
Gate_Func = &TestClass::AND;
break;
case 2:
Gate_Func = &TestClass::OR;
break;
case 3:
Gate_Func = &TestClass::NOT;
break;
default:
break;
}
calculation();
}
void calculation() {
output = (this->*Gate_Func)(InputArr); //C++ expression preceding parentheses of apparent call must have (pointer-to-) function type
}
vector<int> InputArr;
int output = 2;
void printOutput() { cout << output << endl; }
};
int main() {
vector<int> input = { 1, 1 };
TestClass obj(1, input);
obj.printOutput();
}
I want to use different functions depending on the input to calculate the output.
but it says: (expression preceding parentheses of apparent call must have (pointer-to-) function type)
int (TestClass::* Gate_Func)(vector); <<== this is the function I initiated.
and then here: Gate_Func = &TestClass::AND;
I can reference to the AND function with Gate_Func
but why
output = Gate_Func(InputArr);
this one has an error:
C++ expression preceding parentheses of apparent call must have (pointer-to-) function type
#include <iostream>
#include <vector>
using namespace std;
class TestClass {
public:
int AND(vector<int> inputarr) {
return (inputarr[0] == 1 && inputarr[1] == 1);
}
int OR(vector<int> inputarr) {
return (inputarr[0] == 1 || inputarr[1] == 1);
}
int NOT(vector<int> inputarr) {
if (inputarr[0] != 0) return 0;
else return 1;
}
int (TestClass::* Gate_Func)(vector<int>);
TestClass(int ChooseFunction, vector<int> inputarr) {
InputArr = inputarr;
switch (ChooseFunction)
{
case 1:
Gate_Func = &TestClass::AND;
break;
case 2:
Gate_Func = &TestClass::OR;
break;
case 3:
Gate_Func = &TestClass::NOT;
break;
default:
break;
}
}
void calculation() {
output = Gate_Func(InputArr); //C++ expression preceding parentheses of apparent call must have (pointer-to-) function type
}
vector<int> InputArr;
int output = 2;
void printOutput() { cout << output << endl; }
};
int main() {
vector<int> input = { 1, 1 };
TestClass obj(1, input);
obj.printOutput();
}
Oh! I see, thank you guys, so when I use 'this', it's basically like calling the object of the Class and I have to call the function from that object!
Here is the code:
#include <iostream>
#include <vector>
using namespace std;
class TestClass {
public:
int AND(vector<int> inputarr) {
return (inputarr[0] == 1 && inputarr[1] == 1);
}
int OR(vector<int> inputarr) {
return (inputarr[0] == 1 || inputarr[1] == 1);
}
int NOT(vector<int> inputarr) {
if (inputarr[0] != 0) return 0;
else return 1;
}
int (TestClass::* Gate_Func)(vector<int>);
TestClass(int ChooseFunction, vector<int> inputarr) {
InputArr = inputarr;
switch (ChooseFunction)
{
case 1:
Gate_Func = &TestClass::AND;
break;
case 2:
Gate_Func = &TestClass::OR;
break;
case 3:
Gate_Func = &TestClass::NOT;
break;
default:
break;
}
calculation();
}
void calculation() {
output = (this->*Gate_Func)(InputArr); //C++ expression preceding parentheses of apparent call must have (pointer-to-) function type
}
vector<int> InputArr;
int output = 2;
void printOutput() { cout << output << endl; }
};
int main() {
vector<int> input = { 1, 1 };
TestClass obj(1, input);
obj.printOutput();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
函数
AND
、OR
和NOT
是非静态成员函数。仅当提供其类的实例时才能调用它们。Gate_Func
是一个指向非静态成员函数的指针。它可以指向非静态成员函数,例如AND
、OR
或NOT
。为了调用它,您必须提供该类的实例(就像调用AND
、OR
或NOT< /code> 直接)。您还需要向函数提供参数。
这是使用特殊语法完成的:
这里,
p
是指向调用函数的实例的指针,InputArr
是参数。如果有多个参数,它们之间用逗号分隔,就像普通函数调用一样。如果您不记得此语法,也可以执行
std::invoke(Gate_Func, p, InputArr)
。 (实例指针p
位于所有参数之前。)在您的情况下,我怀疑您想在当前实例上调用该函数。您可以使用
this
(例如,(this->*Gate_Func)(InputArr)
)来完成此操作。The functions
AND
,OR
, andNOT
are non-static member functions. They can only be called when an instance of their class is supplied.Gate_Func
is a pointer to non-static member function. It can point to a non-static member function such asAND
,OR
, orNOT
. In order to invoke it, you must supply an instance of the class (just as you would have to do in order to callAND
,OR
, orNOT
directly). You also need to supply the arguments to the function.This is done using a special syntax:
Here,
p
is a pointer to the instance on which to invoke the function, andInputArr
is the argument. If there are multiple arguments, they are separated by commas just like in an ordinary function call.If you can't remember this syntax, you can also do
std::invoke(Gate_Func, p, InputArr)
. (The instance pointerp
goes before all the arguments.)In your case, I suspect you want to invoke the function on the current instance. You can do this by using
this
(e.g.,(this->*Gate_Func)(InputArr)
).指向成员函数的指针需要指向该类型实例 (
this
) 以及参数。提供它的语法相当难看:如果您有权访问 C++17,请使用 std::invoke:
A pointer to a member function requires a pointer to an instance of the type (
this
) as well as the arguments. The syntax for providing it is fairly ugly:If you have access to C++17, use
std::invoke
: