【发布时间】:2019-02-06 22:47:13
【问题描述】:
我有这样的功能:
string inputFormatted(void *errFunc()) {
string input;
cin >> input;
if (input == "") (*errFunc)();
return input;
}
读取一个字符串,如果字符串错误则显示错误
我的 errFunc 是这样实现的:
void errBadKey() {
cout << "Enter a correct key, try HELP command for more information" << endl;
}
我已经以这种方式实现了一个宏:
#define GET_PARAM(input, errorFunc) do { \
input = inputFormatted(errorFunc); \
} while(0);
我是这样使用它的:
int main() {
string test;
GET_PARAM(test, errBadKey);
}
我想知道为什么它说undefined reference to inputFormatted[abi:cxx11](void (*)())
是否不可能将一个函数传递给宏,然后将该函数传递给宏中的其他函数?
【问题讨论】:
-
你为什么使用宏?只需将
GET_PARAM设为函数即可。 -
所以你的意思是这不可能? @NathanOliver
-
这不是不可能的,它应该可以工作,我们需要minimal reproducible example 来告诉你为什么它不工作。那些不是“安全”的宏。它们存在于类型系统之外。使用函数使代码“更安全”。
-
谢谢@NathanOliver,我会使用函数,但我只是想知道是否可以使用宏