英文:
Error when including <msclr/marshal.h> for Winforms project
问题
以下是翻译好的部分:
Good day.
I am making a project using Winforms C++/CLI.
As of now, I am aiming to write a function that will convert the input of the user from a textbox. And from what I could gather, I am basically converting from type System::String^
to type std::string
.
However, whenever I include <msclr/marshal.h>
, I get about 50 errors (here are the first four):
1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\um\objidl.h(12152,71): error C3699: '*' cannot use this indirection on type 'IDataObject'
1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\um\objidl.h(12152,71): message: compiler replacing '*' with '^' to continue parsing
1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\um\objidl.h(12167,5): error C2371: 'IDataObject': redefinition; different basic types
1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\um\objidl.h(590,31): message: see declaration of 'IDataObject'
I have confirmed that the error comes from me including <msclr/marshal.h>
, as the mere inclusion of that header file causes the above errors to pop out.
To resolve my problem, I tried researching for ways to convert System::String^
to std::string
. But, much to my dismay, almost everything revolved around using <msclr/marshal.h>
. And, while there were some solutions that used <locale>
and <codecvt>
, all of them also resulted in a mountain of errors.
I also opened objidl.h
but, I could not even begin to imagine how I could remotely understand what I need to do to fix the code; (1.) Since I did not write it, and (2) I am a noob programmer who is only in his first year of CS.
Finally, all help is appreciated. Thank you very much for your time.
convert.cpp:
#include "convert.h"
#include <string>
#include <msclr/marshal_cppstd.h>
using namespace System;
std::string sysStr_stdStr(String^ value)
{
return msclr::interop::marshal_as<std::string>(value);
}
convert.h:
#ifndef CONVERT_H
#define CONVERT_H
#include <string>
std::string sysStr_stdStr(System::String^ value);
#endif // !CONVERT_H
英文:
Good day.
I am making a project using Winforms C++/CLI.
As of now, I am aiming to write a function that will convert the input of the user from a textbox. And from what I could gather, I am basically converting from type System::String^
to type std::string
.
However, whenever I inlcude <msclr/marshal.h>
, I get about 50 errors (here are the first four):
> 1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\um\objidl.h(12152,71): error C3699: '*': cannot use this indirection on type 'IDataObject'
> 1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\um\objidl.h(12152,71): message : compiler replacing '*' with '^' to continue parsing
> 1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\um\objidl.h(12167,5): error C2371: 'IDataObject': redefinition; different basic types
> 1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\um\objidl.h(590,31): message : see declaration of 'IDataObject'
I have confirmed that the error comes from me including <msclr/marshal.h>
, as the mere inclusion of that header file causes the above errors to pop out.
To resolve my problem, I tried researching for ways to convert System::String^
to std::string
. But, much to my dismay, almost everything revolved around using <msclr/marshal.h>
. And, while there were some solutions that used <locale>
and <codecvt>
, all of them also resulted to a mountain of errors.
I also opened objidl.h
but, I could not even begin to imagine how I could remotely understand what I need to do to fix the code; (1.) Since I did not write it, and (2) I am a noob programmer who is only at his first year of CS.
Finally, all help is appreciated. Thank you very much for your time.
convert.cpp:
#include "convert.h"
#include <string>
#include <msclr/marshal_cppstd.h>
using namespace System;
std::string sysStr_stdStr(String^ value)
{
return msclr::interop::marshal_as<std::string>(value);
}
convert.h:
#ifndef CONVERT_H
#define CONVERT_H
#include <string>
std::string sysStr_stdStr(System::String^ value);
#endif // !CONVERT_H
答案1
得分: -1
好的。所以,我刚刚从使用C++20切换到了C++17语言标准,这对我解决了问题。祝你好运。
英文:
Okay. So, I just switched from using c++20 to c++17 language standard and it resolved the issue for me. Good luck.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论