gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:17:14 24 4
gpt4 key购买 nike

在 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"); 

进程.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));

关于c++ - 在 C++ 中分离声明和初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36806030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com