在 Winforms 项目中包含 <msclr/marshal.h> 时出现错误。

huangapple go评论99阅读模式
英文:

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 &lt;msclr/marshal.h&gt;, I get about 50 errors (here are the first four):

> 1&gt;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&gt;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&gt;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&gt;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 &lt;msclr/marshal.h&gt;, 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 &lt;msclr/marshal.h&gt;. And, while there were some solutions that used &lt;locale&gt; and &lt;codecvt&gt;, 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 &quot;convert.h&quot;
#include &lt;string&gt;
#include &lt;msclr/marshal_cppstd.h&gt;

using namespace System;

std::string sysStr_stdStr(String^ value)
{
	return msclr::interop::marshal_as&lt;std::string&gt;(value);
}

convert.h:

#ifndef CONVERT_H
#define CONVERT_H

#include &lt;string&gt;

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.

huangapple
  • 本文由 发表于 2023年7月3日 21:49:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定