【发布时间】:2013-09-14 03:47:25
【问题描述】:
我在 Visual Studio 2013 预览版中之前的代码中使用了 std::unique_ptr,但我没有遇到问题。对于我最近的项目案例,在Visual Studio 2012中调试时编译器错误较少。所以现在我只收到以下错误
这是有问题的行:
std::unique_ptr<MyClass> classHolder;
以下是编译器所说的
'unique_ptr':不是'std'的成员
语法错误:缺少';'在'
之前
缺少类型说明符 - 假定为 int。注意:C++ 不支持
默认整数';' 之前的意外标记
有没有想过如何解决这个问题?
示例:
#include <memory>
#include <string>
#include <sstream>
#include <boost/weak_ptr.hpp>
#include "JSAPIAuto.h"
#include "MyClass.h"
#ifndef H_CLASSHAVINGPROB
#define H_CLASSHAVINGPROB
class ClassHavingProb : public FB::JSAPIAuto //ClassHavingProb: this is the wrapper class if you are familiar with FireBreath(C++ to Javascript)
{
public:
ClassHavingProb()
{
obj = std::unique_ptr<MyClass>(new MyClass(1));
//MyClass: this is the class reponsible for the functionalities, I've used unique_ptr so that class lifecycle will not be very problematic. If I used a regular pointer the class is not properly dismissed.
//Some more init codes here
}
~ClassHavingProb() {
obj.release(); //the class must be dismissed
}
private:
std::unique_ptr<MyClass> obj;
};
#endif // H_CLASSHAVINGPROB
【问题讨论】:
-
你#include
了吗? -
是的,内存包含在头文件中
-
提供一个完整的例子来说明问题。
-
obj 用作辅助类。
-
你为什么在析构函数中调用
obj.release()?那是内存泄漏。智能指针的全部意义在于您不必像那样手动管理它们。他们负责自己的清理工作。这不是一个完整的例子。
标签: c++ visual-studio-2012 firebreath visual-studio-2013