c++ - std::auto_ptr 错误

标签 c++ auto-ptr

对于下面的 C++ 代码,我得到一个编译器错误:

class Mkt
{
    int k;
public:
    Mkt(int n): k(n)
    {
        throw;
    }
    ~Mkt()
    {
        cout<<"\n\nINSIDE Mkt DTOR function:\t"<<endl;
    }
    void func1()
    {
        cout<<"\n\nINSIDE FUNC1 function....value of k is:\t"<<k<<endl;
    }
};

int main(int argc, char* argv[] )
{
    try
    {
        std::auto_ptr<Mkt> obj(new Mkt(10)); //no implicit conversion
            obj.func1(); //error C2039: 'func1' : is not a member of 'std::auto_ptr<_Ty>'
    }
    catch(...)
    {
        cout<<"\n\nINSIDE EXCEPTION HANDLER..........."<<endl;
    }
return 0;
}

我无法理解为什么会收到错误 C2039?我正在使用 VS 2008 编译器。

请帮忙。 谢谢

最佳答案

它是 auto_ptr,这意味着它是指针 :)。您必须使用 operator->:

obj->func1();

关于c++ - std::auto_ptr 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5484202/

相关文章:

c++ - 如何在不阻塞输入的情况下使用 getline?

c++ - 初始化 std::auto_ptr: "error: no match for call to ‘(std::auto_ptr<int>) (int*)’ "

c++ - 将 shared_ptr 转换为 auto_ptr?

c++ - "most important const"与 auto_ptr : Why the code does not compile?

c++ - 在 Linux 中捕获输入

c++ - boost::interprocess - allocate_aligned - 在所有进程中保证相同的对齐方式?

C++ 为什么不能在 constexpr 函数中调用字符串大小函数

c# - 如果我的程序正在将 dll 文件复制到临时目录,我是否需要特定权限?

c++ - std::auto_ptr<T> 用法

c++ - 从函数返回多个 auto_ptr