std::shared_ptr & boost::shared_ptr 差异

std::shared_ptr & boost::shared_ptr difference

本文关键字:ptr shared 差异 boost std      更新时间:2023-10-16

我有以下代码:

// interface.h
#ifndef INTERFACE_H
#define INTERFACE_H    
#include <memory>    
class IInterface {
public:
    virtual ~IInterface() = 0;
    virtual void doSomething() = 0;
};
inline IInterface::~IInterface() {}
typedef std::shared_ptr< IInterface > IIntPtr;    
IIntPtr makeInterface();    
#endif // INTERFACE_H

// impl.cpp
#include "interface.h"
class Interface : public IInterface { public:
    Interface() {}
    ~Interface() {}
    void doSomething() {} };
IIntPtr makeInterface() { return IIntPtr( new Interface() ); }
// main.cpp
#include "interface.h"
using namespace std;
int main()
{
    auto p = makeInterface();
    p->doSomething();
}

现在:如果我使用typedef std::shared_ptr< IInterface > IIntPtr;都很好,但如果我使用typedef boost::shared_ptr< IInterface > IIntPtr;,我有一些编译器错误:

    编译错误C2027:类型不能使用,直到它被定义。要解决此错误,请确保在引用类型之前已完全定义该类型。
  • 编译错误C2079: 'identifier'使用未定义的类/结构/联合'name'
  • 编译错误C2440:编译器不能从'type1'转换为'type2'.

我正在使用msvc10,但代码必须与msvc9.0编译,所以我必须使用Boos。SmartPtr

我错过了什么吗?

OP在评论中回答了自己的问题:他在尝试Boost版本时忘记了#include <boost/shared_ptr.hpp>