4

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);
    };
}
Dark Falcon
  • 43,592
  • 5
  • 83
  • 98
  • hi hi, cool that you found the error. Even if it's off-topic. The documentation state, that every #pragma managed(push, off) should have happened after the includes, which is obviously not possible for third-party-libraries but for STL headers possible. https://msdn.microsoft.com/en-us/library/0adb9zxe(v=vs.100).aspx – JackGrinningCat Apr 16 '18 at 14:49

2 Answers2

6

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);
    }
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
3

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.

Community
  • 1
  • 1
Dark Falcon
  • 43,592
  • 5
  • 83
  • 98