小编s.p*_*zko的帖子

如何捕获预处理器 #if 条件中未定义的宏?

基于这个问题How to catch empty Defined Macro with gcc? 我还有另一个问题。如何在预处理器#if条件下捕获未定义的宏?示例代码:

#include <stdio.h> 

int main()
{
#if ENABLE_SOMETHING == 1
    ... do something ..
#endif
 return 0;
}
Run Code Online (Sandbox Code Playgroud)

ENABLE_SOMETHING当未使用 gcc 编译器(可能是某个标志)设置时,有没有办法捕获错误/警告?或者也许有我可以使用的外部工具?


我知道我可以写这样的东西:

#ifndef ENABLE_SOMETHING
    #error "ENABLE_SOMETHING undefined!"
#endif
Run Code Online (Sandbox Code Playgroud)

但我在代码中有大量不同的定义(ENABLE_STH1、、...等),我不想手动修复这个问题ENABLE_STH2ENALBE_STH3我正在为我们的项目寻找一些自动解决方案。

c gcc c-preprocessor

10
推荐指数
1
解决办法
4358
查看次数

具有 QObject 继承的单例 - Qt

是否可以使用 QObject 继承创建单例类?添加 QObject 继承后出现编译错误。也许问题在于静态单例创建(应该是动态的)?这是我的方法

标题

#ifndef BLUETOOTHMANAGER_H
#define BLUETOOTHMANAGER_H

#include <QObject>

class BluetoothManager : public QObject
{
    Q_OBJECT
public:
    virtual ~BluetoothManager() {}

    /// Static getter
    static BluetoothManager & GetInstance()
    {
        return instance;
    }

private:
    /// static Bluetooth manager instance
    static BluetoothManager instance;

    explicit BluetoothManager(QObject * parent);
};

#endif // BLUETOOTHMANAGER_H
Run Code Online (Sandbox Code Playgroud)

和cpp文件

#include "BluetoothManager.h"

/// singleton creation
BluetoothManager BluetoothManager::instance = BluetoothManager(static_cast<QObject*>(nullptr));

BluetoothManager::BluetoothManager(QObject * parent)
    : QObject(parent)
{

}
Run Code Online (Sandbox Code Playgroud)

在编译期间我有一个错误

../QtHealthApp/network/bluetooth/BluetoothManager.cpp:4:94: error: use of deleted function ‘BluetoothManager::BluetoothManager(const BluetoothManager&)’  BluetoothManager BluetoothManager::instance = …
Run Code Online (Sandbox Code Playgroud)

c++ qt

2
推荐指数
1
解决办法
1760
查看次数

在函数顶部声明的变量与稍后声明的变量之间的差异

当我在函数顶部定义变量时以及稍后声明它时生成的代码有什么区别.例:

int f(int parameter) {
 int a = parameter * 2;
 if (a == 4) 
  return 1;
 int b = parameter * 4;
 if (b == 4) 
  return 2; 
 return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是否有代码b后变量初始化和分配if (a == 4)或意志ab变量在同一时刻被初始化?

c c++ gcc

1
推荐指数
1
解决办法
120
查看次数

标签 统计

c ×2

c++ ×2

gcc ×2

c-preprocessor ×1

qt ×1