顽固的 IntelliSense 一直标记错误 "function X cannot be called with the given argument list"

Stubborn IntelliSense keeps flagging false "function X cannot be called with the given argument list"

环境

Visual Studio 2013 和 C++/CLI。

(ho/e)错误

我遇到了 IntelliSense 在编译器兼容行上给出错误的情况。因此误报。

错误如下:

IntelliSense: function "< full qualified method name >" cannot be called with the given argument list
argument types are:
< expected argument type >
object type is:
< object type >

发生了什么事

我做了一个UserControl。在那里我用相对委托声明了一个自定义事件。我创建了一个表格。在表单构造函数中,我分配了一个用户控件实例,并尝试将表单方法附加到控件自定义事件。

编译器说一切正常。 IntelliSense 告诉我事件附件类型不匹配。

如何重现

我深入研究了问题并创建了一个应该重现问题的基本上下文:

  1. 创建一个包含两个项目的解决方案:FavaTest(作为 ClassLibrary)和 FavaForm(作为控制台应用程序...或其他)。
  2. FavaTest 中创建一个名称为 FavaClass 的 UserControl 并将以下内容粘贴到 FavaClass.h.

    #pragma once

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;


    namespace FavaTest {

        public ref class FavaClass : public System::Windows::Forms::UserControl
        {
        public:
            FavaClass(void)
            {
                InitializeComponent();
            }

            // -- here defines very simple event --
            delegate void FavaDelegate();
            event FavaDelegate^ FavaEvent;

        protected:
            ~FavaClass()
            {
                if (components)
                {
                    delete components;
                }
            }

        private:
            System::ComponentModel::Container ^components;

    #pragma region Windows Form Designer generated code
            void InitializeComponent(void)
            {
                this->SuspendLayout();
                // 
                // FavaClass
                // 
                this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
                this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
                this->Name = L"FavaClass";
                this->Size = System::Drawing::Size(249, 147);
                this->ResumeLayout(false);

            }
    #pragma endregion
        };
    }

  1. 在项目 FavaForm 中创建一个名为 LaForm 的表单并将以下内容粘贴到 LaForm.h

    namespace FavaForm {

        using namespace System;
        using namespace System::ComponentModel;
        using namespace System::Collections;
        using namespace System::Windows::Forms;
        using namespace System::Data;
        using namespace System::Drawing;

        public ref class LaForm : public System::Windows::Forms::Form
        {
        public:
            LaForm(void)
            {
                InitializeComponent();

                // here simply allocs a FavaClass object and try to attach to FavaEvent event
                FavaTest::FavaClass ^item = gcnew FavaTest::FavaClass();
                item->FavaEvent += gcnew FavaTest::FavaClass::FavaDelegate(this, &LaForm::onfava);
            }

            void onfava(){}

        protected:
            ~LaForm()
            {
                if (components)
                {
                    delete components;
                }
            }
        private:
            System::ComponentModel::Container ^components;

    #pragma region Windows Form Designer generated code
            void InitializeComponent(void)
            {
                this->SuspendLayout();
                // 
                // LaForm
                // 
                this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
                this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
                this->ClientSize = System::Drawing::Size(368, 261);
                this->Name = L"LaForm";
                this->Text = L"LaForm";
                this->ResumeLayout(false);

            }
    #pragma endregion
        };
    }

  1. 建造FavaTest
  2. FavaForm 项目中 common properties 添加对 FavaTest 的新引用,以便将其生成的 dll 作为依赖项使用
  3. 构建解决方案。

现在,虽然编译器预示着一切正常,但您应该看到 IntelliSense 在事件附件行上抱怨了一些事情,并出现以下错误消息:

IntelliSense: function "FavaTest::FavaClass::FavaEvent::add" cannot be called with the given argument list
    argument types are:
(FavaTest::FavaClass::FavaDelegate ^)
    object type is: FavaTest::FavaClass ^

准备运行包裹

我将所有这些打包在一个 side-test-standalone-solution zip 文件中,以便可以解压缩和 运行,但不幸的是(恕我直言,也有疑问)我不能 post此处由于 SE 指南,因此您可以根据上述内容制作调试上下文。

问题

我也可能遗漏了一些东西,但我以前多次使用过这个算法并且它工作得很好,现在我在两台不同的机器(VS2013 和 VS2015)上体验它。这个错误也适用于你吗? IntelliSense 有什么问题?这是一个如此简单的场景,我无法想象我是唯一经历过它的人。但是我在互联网上没有找到任何线索。

已解决。

事实证明,为了避免 IntelliSense 变得疯狂,委托定义必须在事件父 class 范围之外。所以在我的情况下,为了让事情正确(对于 IntelliSense)我必须做的就是将委托移到 class 之外添加 public 关键字,例如:

namespace FavaTest
{

    public delegate void FavaDelegate();    // moved out of the class with "public"

    public ref class FavaClass : public System::Windows::Forms::UserControl
    {
        public:
            [...]

            // -- here defines very simple event --
            // delegate row away from here
            event FavaDelegate^ FavaEvent;

            [...]
    }
}