【问题标题】:A member-function of a managed type cannot be compiled as an unmanaged function托管类型的成员函数不能编译为非托管函数
【发布时间】:2019-05-28 13:04:46
【问题描述】:

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

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

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

SettingsForm.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:

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

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

【问题讨论】:

    标签: c++ c++-cli


    【解决方案1】:

    重现此编译错误:

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

    错误 C3280: 'Test::Test' : 托管类型的成员函数不能编译为非托管函数

    以及大量其他错误。因此,诊断结果是该代码正在编译, /clr 编译选项生效。由于这是一个 .h 文件,因此可能的原因是您将它#include 到一个 .cpp 文件中,而该文件在没有 /clr 的情况下进行编译。您需要找到该#include 指令。

    【讨论】:

    • 该文件在另一个表单的 .h 文件中是 #included。尽管该 .h 文件设置为使用 \clr 进行编译,但它在 #pragma managed(push,off)pragma managed(pop) 之间是 #included。我把它移到这些指令之外,问题就解决了。再次感谢汉斯。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-18
      • 2018-03-19
      • 2012-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多