在C++中分离声明和初始化

Separating declaration and initialisation in C++

本文关键字:初始化 声明 分离 C++      更新时间:2023-10-16

在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();返回一个地址,所以你必须将其存储在指针中。