在C++中分离声明和初始化
Separating declaration and initialisation in C++
在Java中,我通常会这样做来分离声明和初始化:
Object obj;
obj = new Object();
但是,当我尝试将两者分开时,这在C++中不起作用:
unique_ptr<BTSerialPortBinding> bt;
bt = BTSerialPortBinding::Create(dev, 1);
完整的工作声明是:
unique_ptr<BTSerialPortBinding>bt(BTSerialPortBinding::Create(d1.address, 1));
我正在使用这个库:https://github.com/Agamnentzar/bluetooth-serial-port
BTSerialPortBinding::Create(address, channelID)
Returns new instance of BTSerialPortBinding object
address: string containint bluetooth address of the device
channelID: ID of the serial port channel
由于@user1320881的建议,我现在已经尝试了这个,但是当我在头文件中分隔语句声明并在.cpp文件中初始化时,我收到以下错误
1>c:usersjohnipsourcearduinodevice.h(97): error C2143: syntax error: missing ';' before '<' (compiling source file ....SourceArduinoDevice.cpp)
1>c:usersjohnipsourcearduinodevice.h(97): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int (compiling source file ....SourceArduinoDevice.cpp)
1>c:usersjohnipsourcearduinodevice.h(97): error C2238: unexpected token(s) preceding ';' (compiling source file ....SourceArduinoDevice.cpp)
1>c:usersjohnipsourcearduinodevice.cpp(45): error C2065: 'bt': undeclared identifier
1>c:usersjohnipsourcearduinodevice.cpp(45): error C2228: left of '.reset' must have class/struct/union
1>c:usersjohnipsourcearduinodevice.cpp(45): note: type is 'unknown-type'
1>c:usersjohnipsourcearduinodevice.cpp(54): error C2065: 'bt': undeclared identifier
添加这些还会导致其他错误:
ArduinoDevice &ArduinoDevice::operator =(const ArduinoDevice &)': attempting to reference a deleted function
处理中的相关位.cpp文件
dev = ArduinoDevice("/dev/tty.IP-DevB");
过程.h
#ifndef __PROCESS_H
#define __PROCESS_H
#define _USE_MATH_DEFINES
#include "ResonantLowpassFilter.h"
#include "ArduinoDevice.h"
//==============================================================================
/**
*/
class AudioProcessor : public AudioProcessor
{
public:
//==============================================================================
WahwahAudioProcessor();
~WahwahAudioProcessor();
void prepareToPlay (double sampleRate, int samplesPerBlock);
void releaseResources();
void processBlock (AudioSampleBuffer& buffer, MidiBuffer& midiMessages);
AudioProcessorEditor* createEditor();
int getNumParameters();
int getNumPrograms();
int getCurrentProgram();
void setCurrentProgram (int index);
const String getProgramName (int index);
void changeProgramName (int index, const String& newName);
float centreFrequency_, q_;
void updateFilterArduino();
ArduinoDevice dev; //instance to an Arduino device from which sensor data is read
};
#endif // _PROCESS
ArduinoDevice.h
#ifndef ArduinoDevice_h
#define ArduinoDevice_h
#include <stdio.h>
#include "BTSerialPortBinding.h"
class ArduinoDevice
{
public:
ArduinoDevice(const char *dev="");
void connect();
void start(void);
void stop(void);
void read(void);
/**
Disconnects from Arduino device
**/
~ArduinoDevice();
private:
const char *device; //port address of the device (e.g. "/dev/tty.FireFly-E552-SPP")
std::unique_ptr<BTSerialPortBinding> bt; //bt serial port
void close(void);
};
#endif
使用成员reset
分配指向unique_ptr的新指针,并删除以前管理的指针(如果有)。
bt.reset(BTSerialPortBinding::Create(dev, 1));
使用
Object *obj;
obj = new Object();
因为new Object();
返回一个地址,所以你必须将其存储在指针中。
相关文章:
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 在函数内部的声明中初始化数组,并在外部使用它
- 有没有办法一次声明相同类型的多个对象,并通过一个表达式立即使用相同的右值初始化它们?
- 构造函数/函数声明参数列表中的统一初始化
- 使用直接大括号初始化时,C++ 编译错误"声明末尾的预期";"
- 强制使用默认构造函数对成员进行未初始化的声明
- 无法在声明时使用初始值设定项列表初始化常量字符*/字符串数组的向量
- 初始化声明符是 prvalue 表达式吗?
- 智能指针可以单独向前声明和初始化吗?
- 在C++中,如果成员引用在其声明中初始化,为什么需要存储空间?
- C++ 通过函数声明后初始化向量
- C++声明变量时自动类型推断而不初始化
- C++ 警告:在一个声明中将截断从双精度初始化为浮点数,但在另一个声明中则不初始化
- 用初始化列表和超类构造函数声明子类构造函数的正确方式
- 在c++中的复制构造函数/一个声明语句中的初始化的延续中使用chain方法
- public:静态常量字符串声明/初始化问题
- 如何在成员初始化列表中声明共享指针
- c++:何时初始化声明的变量而不将其分配给任何值
- C++ 非整型模板 Const 初始化 预期在类名之前使用初始化声明符
- C++ 模板类编译错误:令牌之前'<'预期的初始化声明器