2012-04-22 9 views
9

Mam 2 funkcje w klasie i otrzymuję błąd w wywołaniu funkcji ParseBits(), tj. "int num_elements = ParseBits(bits, buffer);" z powodu argumentu "bufor", który przekazuję "public int ParseBits(string bits, int* buffer) „:Wskaźniki i bufory o stałym rozmiarze mogą być używane tylko w niebezpiecznym kontekście.

Funkcja 1:

public float AssignFitness(string bits, int target_value) 
     { 

    //holds decimal values of gene sequence 
    int[] buffer = new int[(VM_Placement.AlgorithmParameters.chromo_length/VM_Placement.AlgorithmParameters.gene_length)]; 

    int num_elements = ParseBits(bits, buffer); 

    // ok, we have a buffer filled with valid values of: operator - number - operator - number.. 
    // now we calculate what this represents. 
    float result = 0.0f; 

    for (int i=0; i < num_elements-1; i+=2) 
    { 
     switch (buffer[i]) 
     { 
     case 10: 

      result += buffer[i+1]; 
      break; 

     case 11: 

      result -= buffer[i+1]; 
      break; 

     case 12: 

      result *= buffer[i+1]; 
      break; 

     case 13: 

      result /= buffer[i+1]; 
      break; 

     }//end switch 

    } 

    // Now we calculate the fitness. First check to see if a solution has been found 
    // and assign an arbitarily high fitness score if this is so. 

    if (result == (float)target_value) 

     return 999.0f; 

    else 

     return 1/(float)fabs((double)(target_value - result)); 
    // return result; 
} 

Funkcja 2:

public int ParseBits(string bits, int* buffer) 
     { 

      //counter for buffer position 
      int cBuff = 0; 

      // step through bits a gene at a time until end and store decimal values 
      // of valid operators and numbers. Don't forget we are looking for operator - 
      // number - operator - number and so on... We ignore the unused genes 1111 
      // and 1110 

      //flag to determine if we are looking for an operator or a number 
      bool bOperator = true; 

      //storage for decimal value of currently tested gene 
      int this_gene = 0; 

      for (int i = 0; i < VM_Placement.AlgorithmParameters.chromo_length; i += VM_Placement.AlgorithmParameters.gene_length) 
      { 
       //convert the current gene to decimal 
       this_gene = BinToDec(bits.Substring(i, VM_Placement.AlgorithmParameters.gene_length)); 

       //find a gene which represents an operator 
       if (bOperator) 
       { 
        if ((this_gene < 10) || (this_gene > 13)) 

         continue; 

        else 
        { 
         bOperator = false; 
         buffer[cBuff++] = this_gene; 
         continue; 
        } 
       } 

       //find a gene which represents a number 
       else 
       { 
        if (this_gene > 9) 

         continue; 

        else 
        { 
         bOperator = true; 
         buffer[cBuff++] = this_gene; 
         continue; 
        } 
       } 

      }//next gene 

      // now we have to run through buffer to see if a possible divide by zero 
      // is included and delete it. (ie a '/' followed by a '0'). We take an easy 
      // way out here and just change the '/' to a '+'. This will not effect the 
      // evolution of the solution 
      for (int i = 0; i < cBuff; i++) 
      { 
       if ((buffer[i] == 13) && (buffer[i + 1] == 0)) 

        buffer[i] = 10; 
      } 

      return cBuff; 
     } 

Dostaję 2 błędy na tej funkcji na podświetlonej linii s:

Error 1: The best overloaded method match for 'VM_Placement.Program.ParseBits(string, int*)' has some invalid arguments 

Error 2: Pointers and fixed size buffers may only be used in an unsafe context 
+0

Jakie jest twoje pytanie? dlaczego dostajesz błędy? co możesz zrobić, aby ich uniknąć? dlaczego nie można używać wskaźników w zarządzanym kodzie lub czymś innym? (wiedząc dokładnie, co pomagasz, łatwiej ci pomóc, niż gdybyśmy musieli zgadywać, możemy mieć rację, a może nie). –

+0

Co mogę zrobić, aby ich uniknąć ... Chyba przekazuję argumenty w ParseBits() działa niepoprawnie, a więc dostaję jeszcze jeden błąd "Argument 2: nie można przekonwertować z 'int []' na 'int *'" – user1277070

+0

Dlaczego używasz wskaźników w pierwszej kolejności? – harold

Odpowiedz

5

Może Tęskniłam za to, ale nie wydaje się być cokolwiek, że rzeczywiście wymaga użycia int*. Dlaczego nie po prostu przekazać mu tablicę int i zmienić podpis funkcji ParseBits do:

public int ParseBits(string bits, int[] buffer)

i usunąć unsafe{ ... } bloki całkowicie.

19

Trzeba załączyć funkcję używając surowych wskaźników w bloku unsafe.

unsafe 
{ 
//your code 
} 
+0

i jak wywołać tę funkcję? czyniąc funkcję niebezpieczną usunięto błąd "Wskaźniki i bufory o stałym rozmiarze mogą być używane tylko w niebezpiecznym kontekście", ale wciąż otrzymuję komunikat "Najlepsze przeciążone dopasowanie metody dla" VM_Placement.Program.ParseBits (string, int *) "ma pewne nieprawidłowe argumenty "w tym wywołaniu funkcji. – user1277070

+0

Powiedziałbym, że sprawiłeś, że strona wywoławcza twojej funkcji jest "niebezpieczna", ponieważ wymaga również wskaźników. –

+0

Nopes nie wyszło ... myślę, że to z powodu tego rodzaju kłótni, które mijam ...Dostaję jeszcze jeden błąd do przekazania argumentu podczas wywoływania ParseBits() "Argument 2: nie można przekonwertować z 'int []' na 'int *'" – user1277070

1

Miałem ten sam problem, ale nie rozwiązały go żadne inne odpowiedzi tutaj. Ciągle dostawałem różne błędy.

Niezależnie od tego, jakie funkcje używają niebezpiecznego kodu, po prostu należy je zadeklarować za pomocą "niebezpiecznego" słowa kluczowego.
Na przykład:

static unsafe void myFunction(int* myInt, float* myFloat) 
{ 
    // Function definition 
} 

Osobiście starałem się to zrobić podczas tworzenia klasy otoki.
Dla zainteresowanych, skończyło się patrząc coś takiego:

using System.Runtime.InteropServices; 

namespace myNamespace 
{ 
    public class myClass 
    { 
     [DllImport("myLib.so", EntryPoint = "myFunction")] 
     public static extern unsafe void myFunction(float* var1, float* var2); 
    } 
} 

tam wiele wspaniałych informacji w MSDN „niebezpieczny kod Tutorial”:
https://msdn.microsoft.com/en-us/library/aa288474(v=vs.71).aspx

to chyba warto czytać, ja okazało się to całkiem przydatne.

Powiązane problemy