C++ 传递';系统::字符串';函数
我有下面的函数,希望它能告诉我文件夹是否存在,但是当我调用它时,我得到了这个错误- 无法将参数1从“System::String^”转换为“std::String” 功能-C++ 传递';系统::字符串';函数,c++,string,c++-cli,C++,String,C++ Cli,我有下面的函数,希望它能告诉我文件夹是否存在,但是当我调用它时,我得到了这个错误- 无法将参数1从“System::String^”转换为“std::String” 功能- #include <sys/stat.h> #include <string> bool directory_exists(std::string path){ struct stat fileinfo; return !stat(path.c_str(), &filei
#include <sys/stat.h>
#include <string>
bool directory_exists(std::string path){
struct stat fileinfo;
return !stat(path.c_str(), &fileinfo);
}
有人能告诉我怎么拍这个吗?谢谢。您正在尝试将托管的C++/CLI字符串(
System::string^
)转换为std::string
。没有为此提供隐式转换
为了让它工作,你必须处理这个问题
这可能看起来像:
std::string path = context->marshal_as<std::string>(txtActionFolder->Text));
if(directory_exists(path)) {
this->btnFinish->Enabled = true;
}
要实现这一点,您需要将托管类型
System::String
转换为本机类型std::String
。这涉及到一点封送处理,将导致两个单独的字符串实例。MSDN有一个方便的表格,用于所有不同类型的字符串封送处理
在这种特殊情况下,您可以执行以下操作
std::string nativeStr = msclr::interop::marshal_as<std::string>(managedStr);
std::string nativeStr=msclr::interop::marshal_as(managedStr);
您正在尝试将CLR字符串转换为STL字符串,以将其转换为C字符串,并将其与POSIX仿真函数一起使用。为什么会如此复杂?既然你使用C++ + CLI,就用.< /P>我从来没有看到任何人使用C++/CLI,STL和POSIX函数都在同一个调用…@ Matteo:是的,这是一个相当讨厌的事情……就像我没有用C++ C++过期一样,因此需要寻求帮助!我很感激你可能笑了,但请同情我!!谢谢,第二个例子非常有效。正如你可能猜到的,我不是用C++来表示的(来自互联网的例子),所以这就是为什么事情有点混淆。“大卫维加德意识到这里有两种语言”——C++,和C++ + CLI,这是.NET C++语言绑定。如果您使用的是C++/CLI,我倾向于尽可能多地使用.NET选项…+1作为第一个提到System::IO::Directory::Exists
。我慢慢意识到这是事实,但我希望我还有几个问题!!谢谢
if(System::IO::Directory::Exists(txtActionFolder->Text)) {
this->btnFinish->Enabled = true;
}
std::string nativeStr = msclr::interop::marshal_as<std::string>(managedStr);