【发布时间】:2020-03-21 16:27:10
【问题描述】:
我在执行下面的代码时遇到错误。谁能解释我在做什么错误?
#include <iostream>
using namespace std;
#define one 1
#ifdef one
printf("one id defined");
#endif
void func1();
void __attribute__((constructor)) func1();
void func1()
{
printf("before");
}
int main()
{
cout <<"main";
return 0;
}
以下是我遇到的错误。
prog.cpp:5:11: error: expected constructor, destructor, or type conversion before '(' token
printf("one id defined");
^
【问题讨论】:
-
您不能在任何函数之外执行代码。唯一的例外是对象的初始化。
-
您只能从
main()中调用一个函数,另一个函数或方法。您对printf的调用不在此范围内。 -
在编译期间打印消息:stackoverflow.com/questions/3826832/…
-
如果您解释一下您期望这样做会有所帮助。您预计什么时候会出现输出?
-
另外,这感觉就像您将宏用于它们不打算用于的东西。如果您确实使用了宏,请始终为它们提供完整的大写名称(如 ONE),以避免在几个月后阅读代码时出现混淆!
标签: c++ preprocessor