2015-08-08 27 views
1

我正在Unity c#項目中工作,我得到一個錯誤,我不明白他爲什麼這麼說。爲什麼我的編譯器在將const default添加到構造函數參數後識別某些方法?

enter image description here

這些錯誤指向哪裏調用該方法SaveDictionary()和LoadDictionary()在下列文件中的地方:

using UnityEngine; 
using System.Collections; 
using System.Collections.Generic; 

public class BinaryDictionary<T> 
{ 

    public BinaryDictionary(string name, string path = FileReadWrite.binPath) 
    { 
    fileName = name; 
    this.path = path; 
    } 

    protected Dictionary<string, T> dict; 
    protected string fileName; 
    protected string path; 

    protected virtual void LoadDictionary() 
    { 
    if (dict == null) 
    { 
     try 
     { 
     dict = FileReadWrite.GetBinaryFile<Dictionary<string, T>>(fileName, path); 
     } 
     catch (System.IO.FileNotFoundException e) 
     { 
     Debug.LogWarning(fileName + " not found, making new Dictionary."); 
     dict = new Dictionary<string, T>(); 
     } 
    } 
    } 

    public virtual void SaveDictionary() 
    { 
    if (dict != null) 
    { 
     FileReadWrite.SaveBinaryFile(fileName, path, dict); 
    } 
    } 

    public T GetElement(string key) 
    { 
    LoadDictionary(); 

    T toReturn = dict.ContainsKey(key) ? dict[key] : default(T); 

    return toReturn; 
    } 

    public void AddElement(string key, T value) 
    { 

    LoadDictionary(); 

    if (!dict.ContainsKey(key)) 
    { 
     dict.Add(key, value); 
    } 
    else 
    { 
     dict[key] = value; 
    } 

    SaveDictionary(); 
    } 

    public void RemoveElement(string key) 
    { 
    LoadDictionary(); 
    if (dict.ContainsKey(key)) 
    { 
     dict.Remove(key); 
     SaveDictionary(); 
    } 
    } 

    public Dictionary<string, T>.KeyCollection GetKeys() 
    { 
    LoadDictionary(); 
    return new Dictionary<string, T>(dict).Keys; 
    } 
    public Dictionary<string, T> GetDictionary() 
    { 
    LoadDictionary(); 
    return new Dictionary<string, T>(dict); 
    } 
} 

起初這些錯誤沒有顯示。當我在構造函數中爲參數「path」添加默認值FileReadWrite.binPath時,它們出現了。

我宣佈FileReadWrite.binPath如下:

public const string binPath = "Other/Bin/"; 

當我再次刪除默認值(在構造函數),這些錯誤消失。

有人知道我做錯了什麼,或者他爲什麼說錯誤在這種情況下?

+0

它編譯我,我不得不刪除FileReadWrite.GetBinaryFile和FileReadWrite.SaveBinaryFile調用,也許是錯誤的FileReadWrite。 – Atomosk

+0

FileReadWrite是一個公共靜態類。 –

回答

0

您可以嘗試不使用FileReadWrite.binPath作爲構造函數的默認參數。試試這個,而不是:

public BinaryDictionary(string name, string path = null) 
{ 
    fileName = name; 
    if(path == null) 
    { 
     this.path = FileReadWrite.binPath; 
    } 

} 

我不知道確切的問題,但我想你的虛擬方法導致與你的建設者的衝突。 (不知道雖然,它只是一個猜測)

+0

此代碼有效。但它仍然是一個奇怪的錯誤。 –

+0

重新啓動Unity Editor和Mono。 –

+0

重新啓動,並沒有改變(我正在使用Visual Studio,而不是MonoDevelop :)) –

相關問題