c++ - 当宏用作变量名时,有什么方法可以跳过宏替换(在预处理期间)?

标签 c++ qt c++11 macros qt5

冲突头.h

      #define _c 6 //This is third party header, canot change, since
// there is no sorce code to rebuild

测试类.h

    #ifndef TESTCLASS_H
    #define TESTCLASS_H

    #include <QObject>
    #include "ConflictHeader.h"//Include conflicted header

    class TestClass : public QObject
    {
      Q_OBJECT

    public:
        TestClass(QObject *parent);
        ~TestClass();
    private:

    };
    #endif // TESTCLASS_H

测试类.cpp

#include "testclass.h"

TestClass::TestClass(QObject *parent)
  : QObject(parent)
{

}    
TestClass::~TestClass()
{

}

moc_testclass.cpp

This is generated by MOC compiler, Please note function where compilation issue comes "int TestClass::qt_metacall(QMetaObject::Call _c, int _id, void **_a)"

/****************************************************************************
** Meta object code from reading C++ file 'testclass.h'
**
** Created: Wed Jun 10 11:24:06 2015
**      by: The Qt Meta Object Compiler version 62 (Qt 4.7.4)
**
** WARNING! All changes made in this file will be lost!
*****************************************************************************/

#include "../../testclass.h"
#if !defined(Q_MOC_OUTPUT_REVISION)
#error "The header file 'testclass.h' doesn't include <QObject>."
#elif Q_MOC_OUTPUT_REVISION != 62
#error "This file was generated using the moc from 4.7.4. It"
#error "cannot be used with the include files from this version of Qt."
#error "(The moc has changed too much.)"
#endif

QT_BEGIN_MOC_NAMESPACE
static const uint qt_meta_data_TestClass[] = {

 // content:
       5,       // revision
       0,       // classname
       0,    0, // classinfo
       0,    0, // methods
       0,    0, // properties
       0,    0, // enums/sets
       0,    0, // constructors
       0,       // flags
       0,       // signalCount

       0        // eod
};

static const char qt_meta_stringdata_TestClass[] = {
    "TestClass\0"
};

const QMetaObject TestClass::staticMetaObject = {
    { &QObject::staticMetaObject, qt_meta_stringdata_TestClass,
      qt_meta_data_TestClass, 0 }
};

#ifdef Q_NO_DATA_RELOCATION
const QMetaObject &TestClass::getStaticMetaObject() { return staticMetaObject; }
#endif //Q_NO_DATA_RELOCATION

const QMetaObject *TestClass::metaObject() const
{
    return QObject::d_ptr->metaObject ? QObject::d_ptr->metaObject : &staticMetaObject;
}

void *TestClass::qt_metacast(const char *_clname)
{
    if (!_clname) return 0;
    if (!strcmp(_clname, qt_meta_stringdata_TestClass))
        return static_cast<void*>(const_cast< TestClass*>(this));
    return QObject::qt_metacast(_clname);
}

int TestClass::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
    _id = QObject::qt_metacall(_c, _id, _a);
    if (_id < 0)
        return _id;
    return _id;
}
QT_END_MOC_NAMESPACE

Compiler output error 1>.\GeneratedFiles\Debug\moc_testclass.cpp(62) : error C2143: syntax error : missing ')' before 'constant'

所以我不能在“ConflictHeader.h”(第三方库)和“moc_testclass.cpp”(生成的 moc)中更改“_c”。

当宏被用作变量名时,是否有任何方法可以跳过宏替换(在预处理期间)?

最佳答案

您应该能够使用以下方法解决该问题:

#include "ConflictHeader.h"//Include conflicted header
#ifdef _c
#undef _c
#endif

更好的选择可能是从 ConflictHeader.h 创建一个 wrapper .h 文件并在其余代码中使用 wrappr .h 文件。

ConflictHeaderW.h:

#pragma once

#include "ConflictHeader.h"
#ifdef _c
#undef _c
#endif

// #undef anything else that create problems.

关于c++ - 当宏用作变量名时,有什么方法可以跳过宏替换(在预处理期间)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30747845/

相关文章:

c++ - 使用模板从参数列表创建 std::vector

c++ - 在字符串中为每个第N个字符插入空格的函数无法正常工作吗?

qt - 如何指示 cmake/automoc 查找外部 header

c++ - C++ 中具有可变参数签名的函数映射

c++ - 成员函数的 moveToThread

C++:函数返回指针不应该是 const 的任何原因?

c++ - 图像旋转,伪造图像直方图

c++ - 显示Qt资源中没有的图片

c++ - 基于范围的过对列表

c++ - std::atomic 变量的比较操作线程安全吗?