我的部分代码看起来像这样。
#include <QObject>
#define MYMACRO : public QObject
#define SIGNAL_MACRO Q_OBJECT signals: void testSignal(std::string s);
#define EMIT_MACRO emit testsignal(s);
class myclass MYMACRO
{
SIGNAL_MACRO
...
void myclass::method()
{
std::string s("string");
EMIT_MACRO
}
如果我将宏的内容写入它们应该展开的位置,它就可以正常工作。但是如果我保留宏并让预处理器完成工作,那么我会得到一个“对 myclass::testSignal(std::string) 的 undefined reference ”。我不明白有什么区别。预处理器应该在第一次遇到宏时展开它们,就像我自己写出来的一样。我以为我可以在宏中写任何东西,它会被简单地复制粘贴到需要的地方。
最佳答案
我认为这里的问题可能是 Qt 元对象编译器在 C++ 预处理器之前运行,所以它看不到 SIGNAL_MACRO< 中的
。来自Qt Reference Documentation :Q_OBJECT
/
The moc tool reads a C++ header file. If it finds one or more class declarations that contain the Q_OBJECT macro, it produces a C++ source file containing the meta-object code for those classes.
我的建议是,不要这样做。
关于c++ - Qt4中宏的继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7332383/