基于这个问题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_STH2
。ENALBE_STH3
我正在为我们的项目寻找一些自动解决方案。
是否可以使用 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) 当我在函数顶部定义变量时以及稍后声明它时生成的代码有什么区别.例:
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)
或意志a
和b
变量在同一时刻被初始化?