2013-02-18 38 views
3

我試圖通過Eric Lippert "Why does a recursive lambda cause a definite assignment error?"爲什麼這個錯誤「局部變量不能在此範圍內聲明,因爲它已被使用」?

運行從博客文章的代碼,但不是運行(或給予彙編「明確分配錯誤」我得到:

命名爲「T」的局部變量不能在此範圍內聲明,因爲它 將給予不同的含義,以「T」,這已經是在 的母公司或電流「範圍用來表示別的

爲什麼呢?
它在父級或當前範圍中已經使用了哪些位置?
試圖重命名它已經得到相同的錯誤
我應該如何更改代碼來啓動此代碼?

using System; 
namespace LippertRecursiveLambdaError 
{ 
    class Program 
    { 
    static void Main(string[] args) 
    { 
     Tree<int> tree = new Tree<int> 
     (3, new Tree<int> 
      (4, new Tree<int>(1), null), 
      new Tree<int>(5)); 
     PrintTree(tree); 
     Console.ReadLine(); 
    } 

    delegate void Action<A>(A a); 
    delegate void Traversal<T>(Tree<T> t, Action<T> a); 

    static void DepthFirstTraversal<T>(Tree<T> t, Action<T> a) 
    { 
    if (t == null) return; 
    a(t.Value); 
    DepthFirstTraversal(t.Left, a); 
    DepthFirstTraversal(t.Right, a); 
    } 
    static void Traverse<T>(Tree<T> t, Traversal<T> f, Action<T> a) 
    { 
    f(t, a); 
    } 
    static void Print<T>(T t) 
    { 
    System.Console.WriteLine(t.ToString()); 
    } 
    /*static void PrintTree<T>(Tree<T> t) 
    { 
    Traverse(t, DepthFirstTraversal, Print); 
    }*/ 

    static void PrintTree<T>(Tree<T> t) 
    { 
    //Traversal<T> df = (t, a)=>   ************** 
    Traversal<T> df = null; 
    //======================================== 
//The next line gives compilation error 
//A local variable named 't' cannot be declared in this scope 
//because it would give a different meaning to 't', 
//which is already used in a 'parent or current' scope to denote something else  
    df = (t, 
     a) => 
    { 
     if (t == null) return; 
     a(t.Value); 
     df(t.Left, a); 
     df(t.Right, a); 
    }; 
    Traverse(t, df, Print); 
    }//PrintTree 
    }//class 
    class Tree<T> 
    { 
    public Tree<T> Left; 
    public Tree<T> Right; 
    public T Value; 

    public Tree(T value) 
    { 
     Value = value; 
    } 
    public Tree(T value, Tree<T> left, Tree<T> right) 
    { 
     Value = value; 
     Left = left; 
     Right = right; 
    } 
    } 
}//namespace 
+0

我認爲這是C#,而不是C(或C++或Java)。 – ecatmur 2013-02-18 11:28:40

+0

@ecatmur這絕對是C# – 2013-02-18 11:30:09

+0

這是C#。我不知道java爲什麼/如何跳過標籤。我的錯誤 – Fulproof 2013-02-18 11:32:36

回答

5
static void PrintTree<T>(Tree<T> t) 
    { 
    //Traversal<T> df = (t, a)=>   ************** 
    Traversal<T> df = null; 
    //======================================== 

    df = (t,  a) => 
    { 
     if (t == null) return; 
     a(t.Value); 
     df(t.Left, a); 
     df(t.Right, a); 
    }; 
    } 

這是因爲Tree<T> t是一個聲明 而(t,a) =>是另一個聲明..幾乎同樣的事情,他說:

int someFunction(T t, U a) 
//Assuming int is the return type 

反正來解決:改變t到另一個標識.. n例如

df = (n,a) =>{ 
      if (n == null) return; 
      a(n.Value); 
      df(n.Left, a); 
      df(n.Right, a); 
     }; 
     } 
+0

+1:很好的答案,但我會選擇'n'(代表節點)而不是'u'。 – leppie 2013-02-18 11:34:39

+0

@leppie更改:P – 2013-02-18 11:37:08

+0

Ooops。我試圖以前徒勞地重命名。可能選擇了錯誤的字母(標識符)。神祕......我只是好奇,ppl如何寫這樣的(敲我)代碼而不運行它? – Fulproof 2013-02-18 11:45:35

相關問題