错误 CS7036 没有给定的参数对应于所需的形式参数
Error CS7036 There is no argument given that corresponds to the required formal parameter
我在标题中遇到错误,有人可以告诉我我的代码有什么问题吗?
public class Book
{
public string Distributor { get; set; }
public string Name { get; set; }
public int Amount { get; set; }
public double Price { get; set; }
public Book(string distributor, string name, int amount, double price)
{
this.Distributor = distributor;
this.Name = name;
this.Amount = amount;
this.Price = price;
}
public override string ToString()
{
string line = string.Format("| {0,15} | {1,15} | {2,5} | {3,6} |", Distributor, Name, Amount, Price);
return line;
}
public override bool Equals(object obj)
{
Book book = obj as Book;
return book.Price == Price;
}
public override int GetHashCode()
{
return Price.GetHashCode();
}
public static Book operator >= (Book book1, Book book2) //the error here
{
Book temp = new Book();
if (book1.Name == book2.Name && book1.Price > book2.Price)
temp = book1;
return temp;
}
public static Book operator <= (Book book1, Book book2) // and here
{
Book temp = new Book();
if (book1.Name == book2.Name && book1.Price < book2.Price)
temp = book2;
return temp;
}
}
我在 'operator' 行中遇到错误。我希望运算符 '>=' 和 '<=' 查找具有相同名称且价格更高的书籍。
I Want the operator '>=' and '<=' to find books with the same name and which one costs more.
那些运营商不是这么做的。它们会告诉您一个值是否 less/greater 大于或等于另一个值 。因此,他们应该 return bool
而不是 Book
。如果他们有不同的名字,你还需要决定 return 什么:
public static bool operator >= (Book book1, Book book2)
{
if (book1.Name == book2.Name)
return (book1.Price >= book2.Price);
else
return ?? what do you want to return here ??
}
public static bool operator <= (Book book1, Book book2)
{
if (book1.Name == book2.Name)
return (book1.Price <= book2.Price);
else
return ?? what do you want to return here ??
}
如果这确实是您想要做的,那么我鼓励您也重载 <
和 >
运算符。
我在标题中遇到错误,有人可以告诉我我的代码有什么问题吗?
public class Book
{
public string Distributor { get; set; }
public string Name { get; set; }
public int Amount { get; set; }
public double Price { get; set; }
public Book(string distributor, string name, int amount, double price)
{
this.Distributor = distributor;
this.Name = name;
this.Amount = amount;
this.Price = price;
}
public override string ToString()
{
string line = string.Format("| {0,15} | {1,15} | {2,5} | {3,6} |", Distributor, Name, Amount, Price);
return line;
}
public override bool Equals(object obj)
{
Book book = obj as Book;
return book.Price == Price;
}
public override int GetHashCode()
{
return Price.GetHashCode();
}
public static Book operator >= (Book book1, Book book2) //the error here
{
Book temp = new Book();
if (book1.Name == book2.Name && book1.Price > book2.Price)
temp = book1;
return temp;
}
public static Book operator <= (Book book1, Book book2) // and here
{
Book temp = new Book();
if (book1.Name == book2.Name && book1.Price < book2.Price)
temp = book2;
return temp;
}
}
我在 'operator' 行中遇到错误。我希望运算符 '>=' 和 '<=' 查找具有相同名称且价格更高的书籍。
I Want the operator '>=' and '<=' to find books with the same name and which one costs more.
那些运营商不是这么做的。它们会告诉您一个值是否 less/greater 大于或等于另一个值 。因此,他们应该 return bool
而不是 Book
。如果他们有不同的名字,你还需要决定 return 什么:
public static bool operator >= (Book book1, Book book2)
{
if (book1.Name == book2.Name)
return (book1.Price >= book2.Price);
else
return ?? what do you want to return here ??
}
public static bool operator <= (Book book1, Book book2)
{
if (book1.Name == book2.Name)
return (book1.Price <= book2.Price);
else
return ?? what do you want to return here ??
}
如果这确实是您想要做的,那么我鼓励您也重载 <
和 >
运算符。