none
C++/Cx arrays RRS feed

  • Question

  • Can one of you with experience with C++/Cx tell me why the second case here doesn’t compile:

     

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

    System
    ::String^ path = gcnew System::String(root);array<System::String^>^ dirs = System::IO::Directory::GetDirectories(path);
    for
     (int i = 0; i < dirs->Length; i++)
    {
             // this works
             System::String^ mStr = dirs[i];
             auto nativeString0(msclr::interop::marshal_as<std::string>(mStr));
            

             //this doesn't
             auto nativeString1(msclr::interop::marshal_as<std::string>(dirs[i]));
    }


    The error is:

    1>ManagedConsole.cpp(23): error C2665: 'msclr::interop::marshal_as' : none of the 3 overloads could convert all the argument types
    1>          C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\msclr/marshal_cppstd.h(33): could be 'std::string msclr::interop::marshal_as<std::string,System::String^>(System::String ^const &)'


    1>          while trying to match the argument list '(System::String ^)'
     

     

     



    • Edited by fatcat1111 Thursday, September 4, 2014 6:41 PM
    Thursday, September 4, 2014 5:28 PM

Answers

  • Before finding the answer, check this variant too: marshal_as<std::string>( %*dirs[i] ).


    • Edited by Viorel_MVP Thursday, September 4, 2014 8:27 PM
    • Marked as answer by May Wang - MSFT Friday, September 12, 2014 6:25 AM
    Thursday, September 4, 2014 8:24 PM

All replies

  • Before finding the answer, check this variant too: marshal_as<std::string>( %*dirs[i] ).


    • Edited by Viorel_MVP Thursday, September 4, 2014 8:27 PM
    • Marked as answer by May Wang - MSFT Friday, September 12, 2014 6:25 AM
    Thursday, September 4, 2014 8:24 PM
  • You may get better answers if you correct your title so the right people look at your post.

    Your code is C++/CLI, not C++/Cx.

    --Rob

    Saturday, September 6, 2014 7:28 AM
  • Actually, it could be either.

    http://msdn.microsoft.com/en-us/library/hh699871.aspx


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Saturday, September 6, 2014 1:53 PM
  • Fatcat's code is calling "gcnew" and using .Net framework namespaces. Those are valid for C++/CLI but not for C++/Cx.

    C++/Cx is ref counted, not garbage collected, and allocates with "ref new". C++/Cx is not managed code and doesn't use .Net namespaces.

    Both add the ^ notation to standard C++, but they mean very different things by it.


    Saturday, September 6, 2014 11:04 PM
  • Ack - I should have known that (I do know that, but rushed my reply without thinking carefully enough). Thanks!

    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Sunday, September 7, 2014 2:47 PM
  • Hi fatcat1111,

    I have already used suggestion from Viorel to test the code you provided, it works on my side with VS2013. So I mark the reply as answer.

    If you come back and have a different result, please feel free to let us know.

    May


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, September 12, 2014 6:29 AM