托管类型的成员函数不能编译为非托管函数

A member-function of a managed type cannot be compiled as an unmanaged function

本文关键字:函数 编译 成员 类型 不能      更新时间:2023-10-16

这是我程序中的第2种形式,它生成了上述错误。构造函数是生成错误的原因,我看不出为什么。它与我的主窗口的构造函数几乎相同,它工作得很好。

唯一的区别是这个需要论据。(即使我删除了 SettingsForm 构造函数中的参数,并恢复到void,我仍然会收到相同的错误。

谁能告诉我为什么它似乎认为这个构造函数被编译为非托管功能?

设置表单.h

#pragma once
#pragma managed(push, off)
#include "SpriteyData.h"
#pragma managed(pop)
namespace Spritey {
    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    using namespace SpriteyData;
    /// <summary>
    /// Summary for SettingsForm
    /// </summary>
    public ref class SettingsForm : public System::Windows::Forms::Form
    {
    public:
        SpriteySettings* currentSetLocCopy;//A local copy of our current settings.
        SettingsForm(SpriteySettings* currentSettings)<------ERROR OCCURS HERE
        {
            InitializeComponent();
            currentSetLocCopy = new SpriteySettings(*currentSettings); //take a copy of our current settings
            //initialise the elements on our form to the values stored in the SpriteySettings
            this->anchorDevCheckBox->Checked = currentSetLocCopy->isAnchorDevAllowed();
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~SettingsForm()
        {
            if (components)
            {
                delete components;
            }
            if(currentSetLocCopy)
            {
                delete currentSetLocCopy;
            }
        }
    private: System::Windows::Forms::Button^  CancelButton;
    private: System::Windows::Forms::Button^  ApplyButton;
    private: System::Windows::Forms::GroupBox^  editorSettingsGroup;
    private: System::Windows::Forms::CheckBox^  anchorDevCheckBox;
    private:

注意:以上只是构造函数 + 更多代码,只是导致错误的部分的代码示例。

这也是一个混合托管和非托管项目。

编译错误的重现:

#pragma managed(push, off)
class SpriteySettings {};
ref class Test
{
public:
    Test(SpriteySettings* arg) {}
};

错误 C3280:"Test::Test":无法将托管类型的成员函数编译为非托管函数

以及一系列其他错误。 因此,诊断是此代码是在没有/clr 编译选项的情况下编译的。 由于这是一个 .h 文件,因此可能的原因是您在没有/clr 的情况下编译的.cpp文件中 #including 了它。 您需要找到该 #include 指令。

我也面临着同样的问题。在将 .Net 目标框架版本添加为 v4.5.1(以前缺少)时,该问题已解决