我想将std::list <std::string>
转换为std::list<System::String^>
,但出现错误
&&:不能对带有[_Ty = System :: String ^]
的'_Ty'类型使用此间接寻址
是否可以转换?
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));
}
}
答案 0 :(得分:0)
正如@madreflection所提到的,我固定了我的代码,如下所示。列表必须为System::Collections::Generic::List<System::String^>^
,而不是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();