2013-11-23 44 views
-2

我正在學習C和C#,並且此問題針對C#。我在msdn中查看指針,並且此代碼未編譯,它給出錯誤:錯誤1不能將類型int**隱式轉換爲int*。存在明確的轉換(您是否缺少演員?)。我在這裏錯過了什麼?錯誤不能將類型'int **'隱式轉換爲'int *'。存在明確的轉換(您是否缺少演員?)

下面是代碼:

int ix = 10; 
     unsafe 
     { 
      int* px1; 
      int* px2 = &ix; **The error is on this line** 
     } 

編輯: 這裏是程序將其全部:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Management; 
using System.Diagnostics; 
using System.Windows.Forms; 
using practice; 

class Program 
{ 
    public delegate bool ThisIsTheDelegate(int number); 
    public delegate bool ThisIsAnotherDelegate(int number); 

    private static string address; 

    public static String Address 
    { 
     get 
     { 
      return address; 
     } 
     set 
     { 
      address = value; 
     } 
    } 

    static void Main() 
    { 
     int[] someArray = new int[] { 1, 2, 3, 5, 6, 7, 8, 9, 0 }; 
     foreach (int number in someArray) 
     { 
      Console.WriteLine(number); 
     } 

     int integer = 98; 
     string someString = "edited"; 
     string[] someStr = { "edited" }; 
     String[] anotherStringSomestr = new String[] { "edited" }; 

     var readWithLinq = from far in anotherStringSomestr 
          select far; 

     foreach (var some in readWithLinq) 
     { 
      Console.WriteLine(some); 
     } 

     Program newPro = new Program(); 
     bool isEven1 = newPro.isEven(99); 
     Console.WriteLine(isEven1); 
     ThisIsTheDelegate newDelegate = newPro.isEven; 
     Console.WriteLine(newDelegate(98)); 
     int[] numbers = { 1, 2, 3, 5, 6, 7, 8, 9 }; 
     List<int> evenNumbers = FilterArray(numbers, newDelegate); 
     foreach(int integer1 in evenNumbers) 
     { 
      Console.WriteLine(integer1); 
     } 
     List<int> oddNumbers = FilterArray(numbers, isOdd); 
     foreach (int integer1 in oddNumbers) 
     { 
      Console.WriteLine(integer1); 
     } 

     ThisIsAnotherDelegate anotherDelegate; 
     anotherDelegate = number => (number % 2 == 0); 
     Console.WriteLine("{0} is a even number", anotherDelegate(4)); 

     for (int i = 0; i < someString.Length; i++) 
     { 
      Console.WriteLine(someString); 
     } 

     for (int i = 0; i < someStr.Length; i++) 
     { 
      Console.WriteLine(someStr[i]); 
     } 

     Console.WriteLine(integer); 

     SimpleStruct structss = new SimpleStruct(); 
     structss.DisplayX(); 

     M.x = 1; 
     structss.x = 98; 

     Console.WriteLine(M.x); 
     Console.WriteLine(structss.x); 

     M.structtaker(ref structss); 
     M.classtaker(); 

     Console.WriteLine(structss.x); 
     Console.WriteLine(M.x); 

     M.x = 1; 
     structss.x = 98; 

     int ix = 10; 
     unsafe 
     { 
      int* px1; 
      int* px2 = &ix; 
     } 

     int selection = 98; 
     while (selection != 0) 
     { 
      mainMenu(); 
      Console.Write("Enter choice: "); 
      selection = Convert.ToInt32(Console.ReadLine()); 
      switch (selection) 
      { 
       case 0: 
        break; 
       case 1: 
        openSomething(); 
        break; 
       case 2: 
        calculator(); 
        break; 
       case 3: 
        coolestProgramEverALive(); 
        break; 
       case 4: 
        make_to_do_list(); 
        break; 
       case 5: 
        add_to_do_list(); 
        break; 
       case 6: 
        readToDoList(); 
        break; 
       case 7: 
        linq_and_arrays(); 
        break; 
       case 8: 
        calendar(); 
        break; 
       case 9: 
        linq_and_collections(); 
        break; 
       default: 
        Console.WriteLine("Unkown selection. Try again"); 
        break; 
      } 
     } 
    } 

    private static bool isOdd(int number) 
    { 
     return (number % 2 == 1); 
    } 

    private static List<int> FilterArray(int[] numbers, ThisIsTheDelegate newDelegate) 
    { 
     List<int> result = new List<int>(); 

     foreach (int item in numbers) 
     { 
      if (newDelegate(item)) 
       result.Add(item); 
     } 
     return result; 
    } 

    private static void linq_and_collections() 
    { 
     List<string> names = new List<string>(); 
     names.Add("Billy"); 
     names.Add("Steve"); 
     names.Add("Casandra"); 
     names.Insert(0, "Johanna"); 
     names.Add("Sonny"); 
     names.Add("Suzanne"); 
     names.Insert(2, "Sid"); 

     var queryLinqUpper = from name in names 
          where (name.StartsWith("S") || name.StartsWith("B") || name.StartsWith("J")) 
          let namesToUpper = name.ToUpper() 
          orderby namesToUpper 
          select namesToUpper; 
     foreach (var linqToUpper in queryLinqUpper) 
     { 
      Console.Write(linqToUpper + " "); 
     } 
     Console.WriteLine(); 
     M.WriteTextToConsole("Hello, world. Programming in C# is fun"); 
     char c = 'A'; 
     int count = 14; 
     String str = new String(c, count); 
     str.WriteTextToConsole(); 
     M.WriteTextToConsole(str); 

    } 

    private static void calendar() 
    { 
     Application.Run(new Form1()); 
    } 

    private static void readToDoList() 
    { 
     var files = from file in Directory.GetFiles(@"C:\data", "*.txt") 
        select file; 
     int number = 1; 
     foreach (var file in files) 
     { 
      Console.WriteLine(number + ". " + file); 
      number++; 
     } 
     Console.Write("What todolist do you want to read? Give me the name:"); 
     try 
     { 
      string name = Console.ReadLine(); 
      Address = Path.Combine(@"C:\data", name + ".txt"); 

      TextReader inFile = new StreamReader(Address); 
      while (inFile.Peek() != -1) 
      { 
       string line = inFile.ReadLine(); 
       Console.WriteLine(line); 
      } 
      inFile.Close(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Exception thrown!", "Error"); 
      Console.WriteLine(ex.ToString()); 
     } 
    } 

    private static void linq_and_arrays() 
    { 
     int numberOfElements; 
     Console.WriteLine("Start by setting the int[] array."); 
     Console.Write("How many elements are there in your array?"); 
     numberOfElements = Convert.ToInt32(Console.ReadLine()); 
     int[] array = new int[numberOfElements]; 
     for (int i = 0, j = numberOfElements; i < numberOfElements; i++, j--) 
     { 
      Console.Write("Integers left to add {0}. Enter an integer:", j); 
      array[i] = Convert.ToInt32(Console.ReadLine()); 
     } 
     var arrayquery = from value in array 
         select value; 

     foreach (var val in arrayquery) 
     { 
      Console.WriteLine("Value from array:{0}", val); 
     } 

    } 

    private static void add_to_do_list() 
    { 
     Console.WriteLine("Which todolist do you want to modify?"); 
     listToDoLists(); 
     Console.Write("Enter name of todolist: "); 
     string name = Console.ReadLine(); 
     Address = Path.Combine(@"C:\data", name + ".txt"); 
     String tempString; 
     StreamWriter stream; 
     stream = File.AppendText(Address); 
     Console.Write("Enter your new ToDo: "); 
     tempString = Console.ReadLine(); 
     stream.WriteLine(tempString); 
     stream.Close(); 
     TextReader inFile; 
     inFile = new StreamReader(Address); 
     while (inFile.Peek() != -1) 
     { 
      string line = inFile.ReadLine(); 
      Console.WriteLine(line); 
     } 
     inFile.Close(); 
    } 

    private static void listToDoLists() 
    { 
     int filenumber = 1; 
     string[] filepaths = Directory.GetFiles("C:\\data\\", "*.txt"); 
     foreach (string file in filepaths) 
     { 
      Console.WriteLine(filenumber + ". " + file); 
      filenumber++; 
     } 
    } 

    private static void make_to_do_list() 
    { 
     string path, name; 
     string yesOrNo; 
     Console.Write("Enter name of todolist: "); 
     name = Console.ReadLine(); 
     path = Path.Combine(@"C:\data", name + ".txt"); 
     TextWriter outFile = new StreamWriter(path); 
     labelOne: // else clause : unknown answer 
     Console.WriteLine("Do you want to add something to todolist.Y/N?"); 
     yesOrNo = Console.ReadLine(); 
     if (yesOrNo.ToLower() == "y") 
     { 
      string line; 
      int lines; 
      Console.Write("How many lines?"); 
      lines = Convert.ToInt32(Console.ReadLine()); 
      for (int i = 0; i < lines; i++) 
      { 
       Console.Write("Enter a line of text: "); 
       line = Console.ReadLine(); 
       outFile.WriteLine(line); 
      } 
      outFile.Close(); 
     } 
     else if (yesOrNo.ToLower() == "n") 
     { 
      Console.WriteLine("You can close the application now."); 
     } 
     else 
     { 
      Console.WriteLine("Unknown answer. Try again"); 
      goto labelOne; 
     } 
    } 

    private static void coolestProgramEverALive() 
    { 
     System.Diagnostics.Process.Start(@"C:\Users\KristjanBEstur\Documents\Visual Studio 2012\Projects\The_coolest_program_ever_alive\The_coolest_program_ever_alive\obj\Debug\The_coolest_program_ever_alive.exe"); 
    } 

    private static void calculator() 
    { 
     System.Diagnostics.Process.Start("calc"); 
    } 

    private static void openSomething() 
    { 
     System.Diagnostics.Process.Start("notepad"); 
    } 

    private static void mainMenu() 
    { 
     Console.WriteLine("Main Menu"); 
     Console.WriteLine("0. Quit"); 
     Console.WriteLine("1. OpenSomething"); 
     Console.WriteLine("2. Calculator"); 
     Console.WriteLine("3. coolestProgramEverAlive"); 
     Console.WriteLine("4. Make todolist"); 
     Console.WriteLine("5. Add to todolist"); 
     Console.WriteLine("6. Read to do list"); 
     Console.WriteLine("7. Linq and arrays"); 
     Console.WriteLine("8. Calendar"); 
     Console.WriteLine("9. Linq and collections"); 
    } 


    public bool isEven(int number) 
    { 
     return (number % 2 == 0); 
    } 
} 

static class M 
{ 
    public static int x; 

    public static void WriteTextToConsole(this string text) 
    { 
     Console.WriteLine(text); 
    } 

    public static void structtaker(ref SimpleStruct s) 
    { 
     s.x = 5; 
    } 
    public static void classtaker() 
    { 
     M.x = 5; 
    } 
    } 

class Test 
{ 
    static int value = 20; 
    unsafe static void F(out int* pi1, ref int* pi2) { 
    int i = 10; 
    pi1 = &i; 
    fixed (int* pj = &value) { 
    // ... 
    pi2 = pj; 
    } 
    } 
} 

struct SimpleStruct 
{ 
public int x; 
private int xval; 
public int X 
{ 
    get 
    { 
     return xval; 
    } 
    set 
    { 
     if (value < 100) 
      xval = value; 
    } 
} 
public void DisplayX() 
{ 
    Console.WriteLine("The stored value is: {0}", X); 
} 

}

+4

該代碼本身編譯罰款。它必須是導致問題的一些周圍的代碼。你能擴展你的代碼示例嗎? – JaredPar

+1

你可能在'x'中使用'&x'這是一個指針。但是你的例子並沒有表現出這個問題。 – CodesInChaos

+2

你發佈的完整代碼示例也編譯得很好。 – JaredPar

回答

0

我移動我聲明向下不安全塊內並它修復了它,我不知道爲什麼?

下面是代碼:

unsafe 
    { 
     int ix = 10; 
     int* px1; 
     int* px2 = &ix; 
    } 
+1

這很奇怪。我在VS2012和2013中試過這個,但是無法複製你的錯誤。想要使用C#(和工具)的版本? – Dweeberly

+0

@Dweeberly我正在使用Visual Studio 2012專業版 – somethingSomething

2

我希望我可以複製這一點,似乎是一個相當有趣的問題。下面是我相信適用於本(約格式化抱歉)標準的部分:

18.3 Fixed and moveable variables The address-of operator (§18.5.4) and the fixed statement (§18.6) divide variables into two categories: Fixed variables and moveable variables. Fixed variables reside in storage locations that are unaffected by operation of the garbage collector. (Examples of fixed variables include local variables, value parameters, and variables created by dereferencing pointers.) On the other hand, moveable variables reside in storage locations that are subject to relocation or disposal by the garbage collector. (Examples of moveable variables include fields in objects and elements of arrays.) The & operator (§18.5.4) permits the address of a fixed variable to be obtained without restrictions. However, because a moveable variable is subject to relocation or disposal by the garbage collector, the address of a moveable variable can only be obtained using a fixed statement (§18.6), and that address remains valid only for the duration of that fixed statement. In precise terms, a fixed variable is one of the following: • A variable resulting from a simple-name (§7.6.2) that refers to a local variable or a value parameter, unless the variable is captured by an anonymous function. • A variable resulting from a member-access (§7.6.4) of the form V.I, where V is a fixed variable of a struct-type. • A variable resulting from a pointer-indirection-expression (§18.5.1) of the form *P, a pointer-member-access (§18.5.2) of the form P->I, or a pointer-element-access (§18.5.3) of the form P[E]. All other variables are classified as moveable variables. Note that a static field is classified as a moveable variable. Also note that a ref or out parameter is classified as a moveable variable, even if the argument given for the parameter is a fixed variable. Finally, note that a variable produced by dereferencing a pointer is always classified as a fixed variable.

我花了一些時間,試圖創建一個類似的錯誤,但並不打算。我能夠重現該問題的唯一方法是使用下面的代碼:

void test() { 
     int ix = 10; 
     unsafe { 
      int* px1 = &ix; 
      int* px2 = &px1; // **The error is on this line** 
      } 
     } 

當然這個代碼不能被移動IX申報進入安全範圍固定。也許你可以嘗試在一小段代碼中複製原始問題(如上所述),並驗證問題和修復是否都複製。也許VS變得困惑。我遇到了一些毫無意義的問題,並通過退出VS並重新啓動(不經常但是幾次)而離開。

0

我移動了表達式「int i = 10;」脫離不安全塊,現在編譯。我還將代碼置於另一個vs2012pro實例的新項目中。這也編譯。所以現在我無法複製錯誤。

下面是代碼:

 int ix = 10; 
     unsafe 
     {    
      int* px1; 
      int* px2 = &ix; 
      Test.F(out px1, ref px2); 
      Console.WriteLine("*px1 = {0}, *px2 = {1}", 
       *px1, *px2); // undefined behavior 
     } 

這是其他項目:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace saxerium 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     int ix = 10; 
     unsafe 
     { 
      int* px1; 
      int* px2 = &ix; 
      Test.F(out px1, ref px2); 
      Console.WriteLine("*px1 = {0}, *px2 = {1}", 
       *px1, *px2); // undefined behavior 
     } 
    } 
} 

class Test 
{ 
    static int value = 20; 
    public unsafe static void F(out int* pi1, ref int* pi2) 
    { 
     int i = 10; 
     pi1 = &i; 
     fixed (int* pj = &value) 
     { 
      // ... 
      pi2 = pj; 
     } 
    } 
} 

}

相關問題