오류 컴파일 C ++/CLI Delegate Call with array :: findall ()
-
19-08-2019 - |
문제
다음 코드는 C3867 (... 함수 호출 누락 된 인수 목록 ...) 및 C3350을 초래합니다 (... 대의원 생성자는 2 인수를 기대합니다 ...). 내가 뭘 잘못하고 있죠?
public ref class Form1 : public System::Windows::Forms::Form
{
public:
bool IsEven(int i){
return (i % 2) == 0;
}
Form1(void)
{
numbers = gcnew array<int>{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
};
array<int> ^even = Array::FindAll(
numbers, gcnew Predicate<int>(IsEven));
}
};
해결책
C ++/CLI에서는 기능이 포함 된 유형의 실제 인스턴스를 전달해야합니다.
array<int> ^even = Array::FindAll(
numbers, gcnew Predicate<int>(this, &Test::IsEven));
(또는 당신을 만드십시오 IsEven
방법 static
)
다른 팁
다음 간단한 콘솔 응용 프로그램은 예제를 제공합니다 FindAll()
방법이있는 방법 array
.NET C ++/CLI에서.
이것은 Lambdas를 지원하지 않는 Visual Studio 2005와 함께 작동합니다. 예제는 Windows 양식을 사용하기 때문에이 Windows 콘솔 응용 프로그램에 추가 클래스를 제공하여 Predicate
클래스에서 작동 할 때의 기능 FindAll()
.
이 예제는 술어를 제공하기위한 세 가지 다른 메커니즘을 보여줍니다.
- 객체없이 사용할 수있는 클래스 정적 함수 사용
- 사용하기 전에 객체를 만들어야하는 클래스 메소드를 사용합니다.
- 클래스의 메소드가 아닌 간단한 C 스타일 함수
이것은 매우 기본적인 샘플 사용입니다 int
그러나 더 복잡한 데이터 구조와도 작동합니다.
// _scrap_net.cpp : main project file.
#include "stdafx.h"
using namespace System;
public ref class Thing1
{
private:
int iDiv; // divisor if specified
public:
static bool IsEven(int i){ // static usable without creating an object
return (i % 2) == 0; // even number if division has no remainder
}
static bool IsOdd(int i){ // static usable without creating an object
return (i % 2) != 0; // odd numbered if division has remainder
}
bool IsDivisibleBy (int i) { // non-static must create object before using
return (i % iDiv) == 0; // check if division has remainder
}
bool IsNotDivisibleBy (int i) { // non-static must create object before using
return (i % iDiv) != 0; // check if division has remainder
}
Thing1(void) { iDiv = 2; } // standard constructor
Thing1(int i) { iDiv = i; } // constructor with argument to use IsDivisibleBy()
};
// standalone function used rather than a class function
bool IsLessThan10 (int i) {
return i < 10;
}
int main(array<System::String ^> ^args)
{
// sample array of some integers for our example
array<int> ^numbers = gcnew array<int>{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
};
// our format string to use when printing the array values
String ^ fmt = gcnew String(L" {0,6}");
// use a static function in a class as the predicate so object not needed
array<int> ^even = Array::FindAll(numbers, gcnew Predicate<int>(&Thing1::IsEven));
Console::WriteLine (L"\n even ");
for each (int jj in even) {
Console::Write(fmt, jj);
}
// use a standard function as the predicate so class not needed
array<int> ^lessThan10 = Array::FindAll(numbers, gcnew Predicate<int>(&IsLessThan10));
Console::WriteLine (L"\n lessThan10 ");
for each (int jj in lessThan10) {
Console::Write(fmt, jj);
}
// use a special divisor so create an object with that value and use it.
Thing1 ^ myDiv = gcnew Thing1(3);
// need to specify the object for the object method in the predicate
array<int> ^divBy3 = Array::FindAll(numbers, gcnew Predicate<int>(myDiv, &Thing1::IsDivisibleBy));
Console::WriteLine (L"\n divBy3 ");
for each (int jj in divBy3) {
Console::Write(fmt, jj);
}
// need to specify the object for the object method in the predicate
array<int> ^notDivBy3 = Array::FindAll(numbers, gcnew Predicate<int>(myDiv, &Thing1::IsNotDivisibleBy));
Console::WriteLine (L"\n notDivBy3 ");
for each (int jj in notDivBy3) {
Console::Write(fmt, jj);
}
Console::WriteLine (L"\nEnd");
return 0;
}
이 프로그램의 출력은 다음과 같습니다.
even
2 4 6 8 10 12 14
lessThan10
1 2 3 4 5 6 7 8 9
divBy3
3 6 9 12
notDivBy3
1 2 4 5 7 8 10 11 13 14
End
제휴하지 않습니다 StackOverflow