将System :: String ^转换为std :: strings错误c1083包含文件 [英] Converting System::String^ to std::strings error c1083 include file
本文介绍了将System :: String ^转换为std :: strings错误c1083包含文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
伙计们!
我正在这样做,看是否有一天我可以打印我的数组:
Hi, guys!
I''m doing this, to see if one day I can print my arrays:
// Case_2.cpp
//
#include <stdafx.h>
#include <string>
#include <iostream>
#include <fstream>
#include <istream>
#include "Form1.h"
#include <marshal_cppstd.h> // Also tried "marshal_cppstd.h"
using namespace Case_2;
//
int main ()
{
void save_arrays ();
return 0;
}
//
void save_arrays ()
{
array<String^>^ my_array = gcnew array<String^> {"xxx", "yyy", "zzz"};
for (int i = 0; i< 3; i++)
{
System::String^ clrString = my_array[i];
std::string stdString = marshal_as<std::string>(clrString);
std::cout << stdString << std::endl;
}
return;
}
有帮助吗?
ANY HELP?
推荐答案
除非您有充分的理由,否则应使用宽字符串.
也就是说,您应该使用wstring
和wcout
.
转换将更容易,并且代码可以正确处理ANSI字符集中不可用的字符.
Except if you have a good reason to do otherwise, you should use wide strings.
That is, you should usewstring
andwcout
.
Conversion will be easier and the code will properly works with characters that are not available in ANSI charset.
我对程序进行了一些更改,以使其可以使用Visual Studio 2010来运行. ..在下面找到代码片段:
I have made a little change in your program to make it run by using visual studio 2010... Find the code snippet below:
#include "stdafx.h"
#include <string>
#include <iostream>
#include <fstream>
#include <istream>
#include <msclr\marshal.h>
using namespace System;
using namespace Runtime::InteropServices;
void save_arrays();
int main(array<System::String ^> ^args)
{
save_arrays();
return 0;
}
void save_arrays ()
{
array<String^>^ my_array = gcnew array<String^> {"xxx", "yyy", "zzz"};
for (int i = 0; i< 3; i++)
{
System::String^ clrString = my_array[i];
const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(clrString)).ToPointer();
std::string stdString = chars;
std::cout << stdString << std::endl;
}
return;
}
这篇关于将System :: String ^转换为std :: strings错误c1083包含文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文