从托管代码包含时未定义shared_ptr

发布于 2024-11-25 01:05:43 字数 790 浏览 0 评论 0原文

我正在尝试围绕非托管 C++ 静态库编写托管包装器 (C++/CLI),但遇到两个问题:

  1. 非托管库在其标头中使用 std::shared_ptr 。包含 ,并且标头作为非托管项目的一部分进行编译。但是,当我在托管项目中包含此标头时,我收到此错误:

    错误 C2039:“shared_ptr”:不是“std”的成员

  2. How do I access the Values collection of来自 C++/CLI 的 SortedDictionary?我似乎找不到任何正确语法的示例,并且 C# 样式语法无法编译。

#1 的代码:

// 1>Dummy.h(10): error C2039: 'shared_ptr' : is not a member of 'std'
#pragma once

#pragma managed(push, off)

#include <memory>

class Foo {};
typedef std::shared_ptr<Foo> FooPtr;

#pragma managed(pop)


using namespace System;

namespace Dummy {

    public ref class Class1
    {
    public:
        Class1(FooPtr);
    };
}

I'm trying to write a managed wrapper (C++/CLI) around an unmanaged C++ static library and am having two problems:

  1. The unmanaged library uses std::shared_ptr in its headers. <memory> is included, and the header compiles as part of an unmanaged project. When I include this header in the managed project, however, I receive this error:

    error C2039: 'shared_ptr' : is not a member of 'std'

  2. How do I access the Values collection of a SortedDictionary<K, V> from C++/CLI? I cannot seem to find any examples of the proper syntax, and the C#-style syntax does not compile.

Code for #1:

// 1>Dummy.h(10): error C2039: 'shared_ptr' : is not a member of 'std'
#pragma once

#pragma managed(push, off)

#include <memory>

class Foo {};
typedef std::shared_ptr<Foo> FooPtr;

#pragma managed(pop)


using namespace System;

namespace Dummy {

    public ref class Class1
    {
    public:
        Class1(FooPtr);
    };
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想你的星星会说话 2024-12-02 01:05:43

Shared_ptr 存在于<内存> 中,而不是<功能> 中。使其看起来类似于:

#pragma managed(push, off)
#include <memory>
#include "yourUnmanagedLibrary.h"
#pragma managed(pop)

第二个问题(请只问一个):

using namespace System::Collections::Generic; 
...
    SortedDictionary<int, String^>^ coll = gcnew SortedDictionary<int, String^>;
    coll->Add(1, "one");
    coll->Add(0, "zero");
    for each (String^ value in coll->Values) {
        Console::WriteLine(value);
    }

shared_ptr lives in <memory> not <functional>. Make it look similar to this:

#pragma managed(push, off)
#include <memory>
#include "yourUnmanagedLibrary.h"
#pragma managed(pop)

Second question (ask only one please):

using namespace System::Collections::Generic; 
...
    SortedDictionary<int, String^>^ coll = gcnew SortedDictionary<int, String^>;
    coll->Add(1, "one");
    coll->Add(0, "zero");
    for each (String^ value in coll->Values) {
        Console::WriteLine(value);
    }
你与昨日 2024-12-02 01:05:43

我在这里找到了答案:在 Visual Studio 中编辑 $(IncludePath) "macro" 2010

Visual Studio 2010 包含 Visual Studio 2008 中的标头,该标头没有 shared_ptr

确切的分辨率描述

I have found the answer here: Edit $(IncludePath) "macro" in Visual Studio 2010

Visual Studio 2010 is including the headers from Visual Studio 2008, which do not have shared_ptr.

The exact resolution is described here. The Platform Toolset on the General tab defaulted to vs90. Changing this to vs100 resolved the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文