2

I want to convert std::list <std::string> to std::list<System::String^> and i get error that

&& : Cannot use this indirection on type '_Ty' with [ _Ty=System::String^ ]

Is it possible to convert ?

convert.h

#pragma once

#include <list> 
#include <string>
#include <msclr/marshal.h>
__declspec(dllimport) std::list<std::string> x_browseCan();


__declspec(dllexport) std::list<System::String^> wrap_browse_can();

convert.cpp

#include "pch.h"
#include "cok_main.h"
std::list<System::String^> wrap_browse_can() {
    std::list<std::string> temporary_list;
    std::list<System::String^> object_list;
    temporary_list = x_browseCan();
    for (std::list<std::string>::iterator it = temporary_list.begin(); it != temporary_list.end(); it++) {
        object_list.push_back(msclr::interop::marshal_as<System::String^>(*it));
    }
}
walnut
  • 21,629
  • 4
  • 23
  • 59
Serhan Erkovan
  • 481
  • 4
  • 18
  • 1
    Why have you tagged `C#`? – SᴇM Dec 24 '19 at 07:21
  • Sorry i forgot to add, i return list to c# program @SᴇM – Serhan Erkovan Dec 24 '19 at 07:31
  • 1
    In that case, you need to convert both the string *and* the list, returning `System::Collections::Generic::List^`. – madreflection Dec 24 '19 at 08:03
  • I did what you wrote and errors disappeared , now i get error that ```wrap_browse_can: __declspec(dllexport) cannot be applied to a function wit the __clrcall calling convention ``` @madreflection – Serhan Erkovan Dec 24 '19 at 08:08
  • Now it's a duplicate of https://stackoverflow.com/questions/54708505/c-cli-export-void-return-declspecdllexport-cannot-be-applied-to-a-function – madreflection Dec 24 '19 at 08:10
  • @madreflection i did but still i get error that `std::wstring is not a valid generic argument` , is `wstring` appropriate for `System::Collections::Generic::List<> ` – Serhan Erkovan Dec 24 '19 at 08:27

1 Answers1

1

As mentioned by @madreflection i fixed my code as like below. List has to be System::Collections::Generic::List<System::String^>^ instead of std::list<System::String^>

convert.cpp

#include "pch.h"
#include "cok_main.h"


System::Collections::Generic::List<System::String^>^ wrap_browse_can() {

    std::list<std::string> temporary_list;
    System::Collections::Generic::List<System::String^>^  object_list;
    temporary_list = x_browseCan();

    for (std::list<std::string>::iterator it = temporary_list.begin(); it != temporary_list.end(); it++) {

        std::string asd = *it;
        System::String^ my_string = gcnew System::String(asd.c_str());
        object_list->Add(my_string);
        //object_list.push_back(msclr::interop::marshal_as<System::String^>(asd));

    }
    return object_list;

}

convert.h

#pragma once

#include <list> 
#include <string>
#include <msclr/marshal.h>
__declspec(dllimport) std::list<std::string> x_browseCan();


__declspec(dllexport) System::Collections::Generic::List<System::String^>^ wrap_browse_can();


Serhan Erkovan
  • 481
  • 4
  • 18
  • 2
    Note that `List^` is the .NET version of `std::vector`. The .NET version of `std::list` is `System::Collections::Generic::LinkedList^` – Ben Voigt Dec 25 '19 at 06:57