【问题标题】:unique_ptr compiler error because of importing codes from VS2013 preview to VS2012由于将代码从 VS2013 预览版导入到 VS2012 而导致的 unique_ptr 编译器错误
【发布时间】: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


【解决方案1】:

好吧,由于 FireBreath 必须在没有 C++11 标准的旧浏览器上运行,我不知道如何解决您直接描述的问题,但您可以只使用 boost scoped_ptr 类型unique_ptr。

【讨论】:

    【解决方案2】:

    我在 VS 2013 更新 4 中遇到了同样的问题,我只是写下了

     using namespace std;
    

    它编译了!
    在尝试使用 using 语句之前,我已经包含了 &lt;memory&gt; 并使用了 std::unique_ptr&lt;&gt;(就像您所做的那样),这会产生与您遇到的错误类似的错误。
    但是,当我尝试使用 using namespace std;things 时得到了解决!
    认为这可能对某人有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-06
      • 1970-01-01
      • 1970-01-01
      • 2023-01-05
      • 1970-01-01
      • 2015-01-25
      相关资源
      最近更新 更多