在C++

2016-04-23 55 views
1
分离声明和初始化

在Java中我通常这样做是为了单独的声明和初始化:在C++

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 

分开声明产品添加这些也导致额外的错误:在Process.cpp

ArduinoDevice &ArduinoDevice::operator =(const ArduinoDevice &)': attempting to reference a deleted function 

相关bit文件

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 

回答

0

使用成员reset分配新指针的unique_ptr和删除以前管理的指针,如果有的话。

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

当我声明unique_ptr bt;在我的头文件和您的声明在.cpp文件中我得到以下错误:错误C2238:意外的令牌(S)前';' (编译源文件.. \ .. \ Source \ ArduinoDevice.cpp) – Als

+0

'unique_ptr bt;'在头文件中?它在课堂内吗?因为你不应该在头文件中实例化对象。这也意味着你在头文件中使用了namespace std',而不是在头文件中使用名称空间。使用完整的作用域'std :: unique_ptr ' – Unimportant

+0

我在头文件中放置'unique_ptr bt'来声明它和'bt.reset(BTSerialPortBinding :: Create(dev,1));'在.cpp文件中启动。我应该把std :: unique_ptr 放在头文件中。我编辑了我的答案以包含完整的错误。 – Als

0

使用

Object *obj; 
obj = new Object(); 

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