简体   繁体   English

错误 CS7036 没有给出与所需形式参数相对应的参数

[英]Error CS7036 There is no argument given that corresponds to the required formal parameter

I'm getting the error in the title, can someone tell me what's wrong with my code?我在标题中收到错误,有人可以告诉我我的代码有什么问题吗?

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;
    }
}

I'm getting an error in the 'operator' lines.我在“操作员”行中遇到错误。 I Want the operator '>=' and '<=' to find books with the same name and which one costs more.我希望运算符 '>=' 和 '<=' 找到具有相同名称的书籍以及哪一本成本更高。

I Want the operator '>=' and '<=' to find books with the same name and which one costs more.我希望运算符 '>=' 和 '<=' 找到具有相同名称的书籍以及哪一本成本更高。

That's not what those operators do.那不是那些运营商所做的。 They tell you whether one value is less/greater than or equal to another .它们告诉您一个值是否小于/大于或等于另一个值。 Therefore, they should return bool instead of Book .因此,他们应该返回bool而不是Book You also need to decide what to return if they have different names:如果它们具有不同的名称,您还需要决定返回什么:

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 ??
}

If this is really what you are trying to do, then I would encourage you to also overload the < and > operators.如果这确实是您想要做的,那么我鼓励您也重载<>运算符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误 CS7036:没有给出与 Lärare.Lärare 所需的形式参数“namn”相对应的参数 - Error CS7036: There is no argument given that corresponds to the required formal parameter 'namn' of Lärare.Lärare 错误 CS7036:没有给出与“PlayerMovement.OnFire(InputValue)”的所需形式参数“值”相对应的参数 - Error CS7036: There is no argument given that corresponds to the required formal parameter 'value' of 'PlayerMovement.OnFire(InputValue)' 错误 CS7036:没有给出与“Vector2.Vector2(float, float)”所需的形参“y”相对应的参数 - error CS7036: There is no argument given that corresponds to the required formal parameter 'y' of 'Vector2.Vector2(float, float)' 错误 CS7036 没有给出对应于“UserInput.UserChoice”所需形式参数“ComputerChoice”的参数 - Error CS7036 There is no argument given that corresponds to the required formal parameter “ComputerChoice” of “UserInput.UserChoice” C# 没有给出与“Receiver(NetworkStream) 错误 CS7036 - C# No argument given that corresponds to the required formal parameter 'stream' of 'Receiver(NetworkStream) Error CS7036 CS7036 C#没有给出与c#所需形式参数相对应的参数 - CS7036 C# There is no argument given that corresponds to the required formal parameter of c# 错误 CS7036:没有给出与所需形式参数“播放器”相对应的参数无法找出代码的问题? - error CS7036: There is no argument given that corresponds to the required formal parameter 'player' can't figure out the issue with the code? 尝试创建 class 的新实例时,获取 CS7036 C# 没有给出与所需的形式参数相对应的参数 - When trying to create a new instance of a class get CS7036 C# There is no argument given that corresponds to the required formal parameter of 错误:没有给出与所需形式参数相对应的参数 - Error: There is no argument given that corresponds to the required formal parameter CS7036:没有给定与...相对应的参数(以前起作用的代码行突然不起作用) - CS7036: No argument given that corresponds to… (line of code that previously worked suddenly not working)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM