2009-03-16 33 views
6

Kết quả mã sau trong C3867 (... function call missing argument list ...) và C3350 (... một đại biểu constructor dự kiến ​​2 đối số (S)...). Tôi đang làm gì sai?Lỗi biên dịch C++/CLI Cuộc gọi ủy nhiệm sử dụng Predicate with Array :: FindAll()

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)); 
     } 
    }; 

Trả lời

11

Trong C++/CLI bạn phải vượt qua những ví dụ thực tế của các loại có chứa các chức năng:

array<int> ^even = Array::FindAll(
    numbers, gcnew Predicate<int>(this, &Test::IsEven)); 

(hoặc làm phương pháp của bạn IsEvenstatic)

+0

Và tôi cũng quên dấu và. Cảm ơn. –

0

Việc áp dụng giao diện điều khiển đơn giản sau đây cung cấp ví dụ về phương pháp FindAll() với một array trong .NET C++/CLI.

Điều này làm việc với Visual Studio 2005 không cung cấp hỗ trợ cho lambdas. Vì ví dụ của bạn đang sử dụng Windows Forms, tôi cung cấp một lớp bổ sung trong ứng dụng bảng điều khiển Windows này để hiển thị hàm Predicate từ một lớp khi được sử dụng trong FindAll().

Ví dụ này cho thấy ba cơ chế khác nhau để cung cấp một vị:

  • sử dụng một hàm tĩnh lớp có sẵn mà không có một đối tượng
  • sử dụng một phương pháp học đòi hỏi một đối tượng được tạo ra trước khi sử dụng
  • một đơn giản Hàm kiểu C không phải là phương thức trong một lớp

Đây là mẫu rất cơ bản khi sử dụng int tuy nhiên nó cũng có ork với cấu trúc dữ liệu phức tạp hơn.

// _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; 
} 

Kết quả từ chương trình này trông giống như sau:

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 
Các vấn đề liên quan