在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:\users\john\ip\source\arduinodevice.h(97): error C2143: syntax error: missing ';' before '<' (compiling source file ..\..\Source\ArduinoDevice.cpp)
1>c:\users\john\ip\source\arduinodevice.h(97): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int (compiling source file ..\..\Source\ArduinoDevice.cpp)
1>c:\users\john\ip\source\arduinodevice.h(97): error C2238: unexpected token(s) preceding ';' (compiling source file ..\..\Source\ArduinoDevice.cpp)
1>c:\users\john\ip\source\arduinodevice.cpp(45): error C2065: 'bt': undeclared identifier
1>c:\users\john\ip\source\arduinodevice.cpp(45): error C2228: left of '.reset' must have class/struct/union
1>c:\users\john\ip\source\arduinodevice.cpp(45): note: type is 'unknown-type'
1>c:\users\john\ip\source\arduinodevice.cpp(54): error C2065: 'bt': undeclared identifier
添加这些还会导致其他错误:
ArduinoDevice &ArduinoDevice::operator =(const ArduinoDevice &)': attempting to reference a deleted function
Process.cpp文件中的相关位
dev = ArduinoDevice("/dev/tty.IP-DevB");
进程
#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
Arduino设备.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));
关于c++ - 用C++分隔声明和初始化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36806030/