2010-06-15 74 views
1

我是編程的初學者。那麼你能告訴我如何爲你的compile()方法傳遞值。傳遞方法的值

class CL 
{ 

    private const string clexe = @"cl.exe"; 
    private const string exe = "Test.exe", file = "test.cpp"; 
    private string args; 
    public CL(String[] args) 
    { 
     this.args = String.Join(" ", args); 
     this.args += (args.Length > 0 ? " " : "") + "/Fe" + exe + " " + file; 
    } 

    public Boolean Compile(String content, ref string errors) 
    { 
     //remove any old copies 
     if (File.Exists(exe)) 
      File.Delete(exe); 
     if (File.Exists(file)) 
      File.Delete(file); 

     File.WriteAllText(file, content); 

     Process proc = new Process(); 
     proc.StartInfo.UseShellExecute = false; 
     proc.StartInfo.RedirectStandardOutput = true; 
     proc.StartInfo.RedirectStandardError = true; 
     proc.StartInfo.FileName = clexe; 
     proc.StartInfo.Arguments = this.args; 
     proc.StartInfo.CreateNoWindow = true; 

     proc.Start(); 
     //errors += proc.StandardError.ReadToEnd(); 
     errors += proc.StandardOutput.ReadToEnd(); 

     proc.WaitForExit(); 

     bool success = File.Exists(exe); 

     return success; 
    } 
} 
+0

欲瞭解更多信息..... http://stackoverflow.com/questions/3036238/how-to-integrate-c- compiler-in-visual-studio-2008 – Kasun 2010-06-15 06:01:09

回答

1
public Boolean Compile(String content, ref string errors) 

你想知道如何稱呼呢?嘗試。 。 。

string content = "#include <stdio.h>\nmain(){\nprintf(\"Hello world\");\n}\n"; 
string errors = ""; 

CL k = new CL(new string[2] {"/Od", "/C"}); 
if(k.Compile(content, ref errors)) 
    Console.WriteLine("Success!"); 
else 
    Console.WriteLine("Failure: {0}", errors); 

希望這有助於

+0

感謝您的幫助.... :)。但它會引發此錯誤... 錯誤方法'CL'沒有重載'0'參數 – Kasun 2010-06-15 06:20:39

+0

嗨,仍然無法編譯它。發生以下錯誤............... 方法'CL'沒有超載需要'0'參數 – Kasun 2010-06-15 06:42:25

+0

道歉,我錯過了CL的構造函數的參數。我添加了兩個僞造的(/ Od =禁用優化,/ C =不刪除註釋)。 – 2010-06-15 07:32:28

1

使用設計器創建一個表單,添加一個文本框將其命名爲txtCplusplus和一個按鈕。爲該按鈕添加一個點擊事件。

將您的CL類粘貼到與事件處理程序(form.cs或其他任何您稱之爲的內容)相同的文件中,而不是在方法或屬性中。

中的按鈕單擊事件處理程序把這個代碼:

 
     CL cmp = New CL(); 
     string errs; 
     if (cmp.Compile(txtCplusplus.Text, ref errs) { 
     MessageBox.Show("Success"); 
     } else { 
     MessageBox.Show(errs); 
     } 
+0

感謝您的幫助.... :)。但它會拋出這個錯誤...錯誤1方法'CL'沒有超載需要'0'參數 – Kasun 2010-06-15 06:29:46

+0

CL不是一種方法,它是一個類。這意味着你沒有正確定義類。 Hmn,看看你發佈的內容,我敢打賭,你有行「CL k = new CL() k .Compile();」包括在內。如果是這樣,那兩條線就是問題所在。擺脫他們。 – jmoreno 2010-06-15 06:47:09

+0

我按你所說的完成了。但仍然是同樣的問題.. – Kasun 2010-06-15 06:50:04