Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 委托使用C++/CLI_.net_Visual Studio_Function_Delegates_Intellisense - Fatal编程技术网

.net 委托使用C++/CLI

.net 委托使用C++/CLI,.net,visual-studio,function,delegates,intellisense,.net,Visual Studio,Function,Delegates,Intellisense,我试图在C++/Cli中使用委托,我参考了C#中的视频 #包括 #包括“stdafx.h” 使用名称空间系统; 使用命名空间System::Collections::Generic; 公共参考类员工 { 公众: 雇员(){}; ~Employee(){}; 公众: int-ID; 字符串^name; 国际工资; 国际经验; 公共:静态无效升级(列表^members,promoteDelegate^promo) { 每个(成员中的员工^emp) { if(促销(emp)) 控制台::WriteLi

我试图在C++/Cli中使用委托,我参考了C#中的视频

#包括
#包括“stdafx.h”
使用名称空间系统;
使用命名空间System::Collections::Generic;
公共参考类员工
{
公众:
雇员(){};
~Employee(){};
公众:
int-ID;
字符串^name;
国际工资;
国际经验;
公共:静态无效升级(列表^members,promoteDelegate^promo)
{
每个(成员中的员工^emp)
{
if(促销(emp))
控制台::WriteLine(“{0}已升级”,emp->name);
}
}
};
代表bool升级员工(员工^emp);
参考类MyClass
{
公众:
MyClass(){};
~MyClass(){};
静态布尔函数(员工^emp)
{
返回(emp->薪资>=5000);
}
int main(数组^args)
{
列表^Employee\u List=gcnew List;
员工^emp1=gc新员工;
emp1->name=“Ajanth”;emp1->ID=0;emp1->Salary=50000;emp1->Experience=3;
员工^emp2=gc新员工;
emp2->name=“Aman”;emp2->ID=1;emp2->Salary=45000;emp2->Experience=9;
员工^emp3=gc新员工;
emp3->name=“Ashish”;emp3->ID=2;emp3->Salary=60000;emp3->Experience=4;
员工列表->添加(emp1);
员工列表->添加(emp2);
员工列表->添加(emp3);
promoteDelegate^promotedel=gcnewpromotedelegate(func);
Employee::Promote(Employee_List,promotel);//此处出错
返回0;
}
};
我得到的编译器错误如下

“Employee::Promote”不接受2个参数。无法使用给定的参数列表调用函数“Employee::Promote”。
参数类型为:(System::Collections::Generic::list^,promoteDelegate^


始终从错误列表的顶部开始。以后的消息越来越不可靠。您将得到的第一个错误是Promote()函数定义,编译器还不知道什么是
promoteDelegate
。这会导致更多错误,如您引用的错误

记住的重要规则是,与C语言不同,C++编译器使用单遍编译。所有类型都必须在使用之前声明。这可能很尴尬,因为它在这里,因为委托类型本身使用雇员类型。必须用前向声明解决的“鸡和蛋”问题:

还要修复您的#includes,stdafx.h必须始终首先包含

#include <iostream>
#include"stdafx.h"

using namespace System;
using namespace System::Collections::Generic;


public ref class Employee
{
public:
    Employee() {};
    ~Employee() {};
public:
    int ID;
    String ^name;
    int Salary;
    int Experience;

public: static void Promote(List<Employee^>^ members,promoteDelegate^ promo)
{
    for each (Employee ^emp in members)
    {
        if(promo(emp))
        Console::WriteLine("{0} is Promoted", emp->name);
    }
}

};

delegate bool promoteDelegate(Employee^ emp);

ref class MyClass
{
public:
    MyClass() {};
    ~MyClass() {};

static bool func(Employee^ emp)
{
    return (emp->Salary >= 5000);
}

int main(array<System::String ^> ^args)
{
    List<Employee^> ^Employee_List = gcnew List<Employee^>;
    Employee^ emp1 = gcnew Employee;
    emp1->name = "Ajanth"; emp1->ID = 0;emp1->Salary = 50000;emp1->Experience = 3;

    Employee^ emp2 = gcnew Employee;
    emp2->name = "Aman"; emp2->ID = 1;emp2->Salary = 45000;emp2->Experience = 9;

    Employee^ emp3 = gcnew Employee;
    emp3->name = "Ashish"; emp3->ID = 2;emp3->Salary = 60000;emp3->Experience = 4;

    Employee_List->Add(emp1);
    Employee_List->Add(emp2);
    Employee_List->Add(emp3);

    promoteDelegate ^promoDel = gcnew promoteDelegate(func);

    Employee::Promote(Employee_List, promoDel); //Error here
    return 0;
}
};
ref class Employee;    // Forward declaration
public delegate bool promoteDelegate(Employee^ emp);

public ref class Employee
{
   // etc..
}