2010-09-07 5 views
5

umfassen "stdafx.h"

#include <string> 
#include <msclr/marshal_cppstd.h> 

ref class Test { 
    System::String^ text; 
    void Method() { 
     std::string f = msclr::interop::marshal_as<std::string>(text); // line 8 
    } 
}; 

Dieser Code, wenn sie mit VS2008 kompiliert gibt:marshal_as, Streicher und Felder vs. Eigenschaften

.\test.cpp(8) : error C2665: 'msclr::interop::marshal_as' : none of the 3 overloads could convert all the argument types 
     f:\programy\vs9\vc\include\msclr\marshal.h(153): could be '_To_Type msclr::interop::marshal_as<std::string>(const char [])' 
     with 
     [ 
      _To_Type=std::string 
     ] 
     f:\programy\vs9\vc\include\msclr\marshal.h(160): or  '_To_Type msclr::interop::marshal_as<std::string>(const wchar_t [])' 
     with 
     [ 
      _To_Type=std::string 
     ] 
     f:\Programy\VS9\VC\include\msclr/marshal_cppstd.h(35): or  'std::string msclr::interop::marshal_as<std::string,System::String^>(System::String ^const &)' 
     while trying to match the argument list '(System::String ^)' 

Aber wenn ich das Feld in Eigenschaft ändern:

property System::String^ text; 

dann kompiliert dieser Code ohne Fehler. Warum?

Antwort

5

Fehler, in VS2010 behoben. Rückmeldungsposition is here.

+2

Scheint nicht in VS2010 behoben werden (zumindest funktionierte es nicht in meinem Code), aber die Abhilfe arbeitete für mich. Vielen Dank! – Paul

+0

Hat auch nicht in meinem Code funktioniert, aber diese Problemumgehung hat auch meine behoben. Vielen Dank! – casper

+0

In der Tat, noch nicht behoben, und passiert in anderen Situationen wie verwaltete Array-Elemente Marshalling: Marshal_as (ArrayOfSystemStrings [i]). Die Problemumgehung besteht darin, eine Dummy-Zwischenvariable zu erstellen: System :: String^dummy = ArrayOfSystemStrings [i]; std :: string s = marshal_as (Dummy); – Pragmateek

6

Eine Abhilfe ist, eine Kopie wie diese zu machen:

ref class Test { 
    System::String^ text; 
    void Method() { 
     System::String^ workaround = text; 
     std::string f = msclr::interop::marshal_as<std::string>(workaround); 
    } 
}; 
+2

Ich bin gerade mit VS2013.3 in das hineingeraten - scheint immer noch ein Problem zu sein. Die beschriebene Problemumgehung funktioniert immer noch. – Niall

1

Ich verwende diesen Schnipsel viel, und es ist zu viel Unordnung eine neue Variable zu deklarieren. Das funktioniert aber auch:

Eine andere Option, die für mich funktioniert, ist diese kleine Vorlage. Es löst nicht nur den hier besprochenen Fehler, sondern behebt auch eine andere Sache, die mit marshal_as gebrochen ist, nämlich dass es nicht für die Eingabe von nullptr funktioniert. Aber eigentlich wäre eine gute C++ - Übersetzung für ein nullptr System :: String eine .empty() std :: string(). Hier ist die Vorlage:

template<typename ToType, typename FromType> 
inline ToType frum_cast(FromType s) 
{ 
    if (s == nullptr) 
     return ToType(); 
    return msclr::interop::marshal_as<ToType>(s); 
} 
Verwandte Themen