2013-10-22 44 views
4

我該如何解決這個錯誤?如何解決「方法沒有超載」需要0個參數「?

「方法'輸出沒有超載需要0個參數」。

錯誤在「fresh.output();」的最底部。

我不知道我在做什麼錯。有人能告訴我我應該怎麼做才能修復代碼嗎?

這裏是我的代碼:

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

namespace ConsoleApplication_program 
{ 
    public class Numbers 
    { 
     public double one, two, three, four; 
     public virtual void output(double o, double tw, double th, double f) 
     { 
      one = o; 
      two = tw; 
      three = th; 
      four = f; 
     } 
    } 
    public class IntegerOne : Numbers 
    { 
     public override void output(double o, double tw, double th, double f) 
     { 
      Console.WriteLine("First number is {0}, second number is {1}, and third number is {2}", one, two, three); 
     } 
    } 
    public class IntegerTwo : Numbers 
    { 
     public override void output(double o, double tw, double th, double f) 
     { 
      Console.WriteLine("Fourth number is {0}", four); 
     } 
    } 
    class program 
    { 
     static void Main(string[] args) 
     { 
      Numbers[] chosen = new Numbers[2]; 

      chosen[0] = new IntegerOne(); 
      chosen[1] = new IntegerTwo(); 

      foreach (Numbers fresh in chosen) 
      { 
       fresh.output(); 
      }  
      Console.ReadLine(); 
     } 
    } 
} 
+1

我想不出一個更全面的錯誤信息......什麼是你不明白? –

+0

fresh.output();傳遞參數,它應該像output(double o,double tw,double th,double f); – Anand

+1

錯誤信息不清楚? –

回答

10

它告訴你方法「輸出」需要的參數。這裏是「輸出」的簽名:

public override void output(double o, double tw, double th, double f) 

所以,如果你想打電話,你需要通過四個雙打。

fresh.output(thing1,thing2,thing3,thing4); 

或者用硬編碼值爲例:

fresh.output(1,2,3,4); 
+0

我把它改成這樣:「fresh.output(double o,double tw,double th,double f);」但我得到了更多的錯誤。你能幫忙嗎? – User

+0

當你調用一個方法時,你傳入實例。您不必再次聲明類型。這是在你聲明方法時完成的。您需要了解調用方法和聲明方法之間的區別。 –

0

你調用output方法0(零)的參數,但是你已經宣佈它接受4個雙值。編譯器不知道應該調用什麼,因爲沒有參數沒有output方法。

0

方法output的所有實現都需要參數。提供參數,你應該能夠編譯。

像這樣:

fresh.output(1, 2, 3, 4); 
3

有沒有名爲output方法來獲取0參數的話,只有一個接受4個參數。你必須傳遞參數給output()

foreach (Numbers fresh in chosen) 
{ 
    fresh.output(o, tw, th, f); 
} 
0

fresh.output()預計2個參數,你不爲他們提供

相關問題