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

时间:2016-04-23 02:39:02

标签: c++ pointers initialization declaration

在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的建议,但是当我在头文件&amp;中分隔语句声明时,我得到以下错误:在.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"); 

Process.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

2 个答案:

答案 0 :(得分:0)

使用成员reset分配一个指向unique_ptr的新指针,并删除以前管理的指针(如果有)。

bt.reset(BTSerialPortBinding::Create(dev, 1));

答案 1 :(得分:0)

使用

Object *obj;
obj = new Object();

因为new Object();返回一个地址,所以你必须将它存储在指针中。