我创建了自己的LOGGER,在其中我为过载宏使用了另一个类。有#define qlcd MacroCall(QLC :: Debug),所以我可以像这样使用记录器:qlcd 可以,但是当我尝试使用qlcd(“ log name”)时出现错误。看最少的代码(为简化起见,没有宏):#include <QVariant>#include <QDebug>class QLC{public:// Error typesenum LevelType{ Debug=0, // Debug Error=1, // Error WTF = 2 // WTF???} level;QString logger;// ConstructorsQLC(QLC::LevelType l) :level(l), logger(":"){}QLC(QLC::LevelType l, QString log) :level(l), logger(log){}// OPERATOR <<QLC& operator<<(const QVariant var){ qDebug() << "(" + QString::number(level) + ")" << logger << var;}};class MacroCall{QLC::LevelType level;public:MacroCall() :level(QLC::Debug){}MacroCall(int i) :level(QLC::WTF){}MacroCall(QLC::LevelType l) :level(l){}QLC operator()(){ return QLC(level);}QLC operator()(QString log){ return QLC(level, log);}};int main(int argc, char*argv[]){MacroCall()("WorkLog") << "No level, yes logname";MacroCall(QLC::Error)() << "No logname, yes level";MacroCall a(QLC::Error);a("WorkLog") << "Logname and level at different lines";// GET READY!// INT as level and logname:MacroCall(2)("WorkLog") << "WTF?? It works!";//MacroCall(QLC::WTF)("NotWorkLog") << "It's not work!!!!!!";// NOT WORK: error: invalid use of qualified-name 'QLC::WTF'// Qt 4.8.3return 0;} (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 代码  MacroCall(QLC :: WTF)(“ NotWorkLog”)被解释为变量的声明:MacroCall QLC::WTF("NotWorkLog")例:class A{ public: enum E { x=1 }; public: A(E) {} void operator()(int) const { }};class B {};void f(){ (A(A::x))(1); // Ok A{A::x}(1); // Ok A(a)(A::x); // Ok A::E a; // ‘a’ has a previous declaration as ‘A a’ A(A::x)(1); // invalid use of qualified-name ‘A::x’ B(b)(A::x); // no matching function for call to ‘B::B(A::E)’} (adsbygoogle = window.adsbygoogle || []).push({});
10-08 09:29