简体   繁体   English

尝试在字符串上重载运算符时收到错误

[英]Receiving error on attempt to overload operator on string

I know this can be done in C++, but I'm not sure what to look for with this. 我知道这可以在C ++中完成,但是我不确定该用什么。 Basically I have a string value in a custom class named Priority . 基本上,我在名为Priority的自定义类中有一个string值。 What I would like to do is add a custom operator to allow Priority++ or Priority-- in which a few things happen. 我想做的是添加一个自定义运算符,以允许Priority++Priority--发生一些事情。 The operation would look like this... 该操作将如下所示:

public static -- ()
{
    Int32 priority = Convert.ToInt32(value);
    priority--;
    string returnValue = priority.ToString();
    return returnValue;
}

and the opposite for ++ . ++则相反。 What I have can be found below, however I receive the error Priority is a property, but is used like a type. 我所拥有的可以在下面找到,但是我收到错误Priority is a property, but is used like a type.

Existing erred code: 现有的错误代码:

public static Priority operator ++(string value)
{
    Int32 priority = Convert.ToInt32(value);
    priority--;
    string returnValue = priority.ToString();
    return returnValue;
}

take a step back and consider : 退后一步考虑:

int i = 0;
i++;

the ++ operator here takes an int, and returns an updated int. 这里的++运算符采用一个int,并返回一个更新的int。

I have a simple class: 我有一堂简单的课:

public class MyClass
{
    public string Test { get; set; }

    public static MyClass operator ++(MyClass myclass)
    {
        myclass.Test += " test";
        return myclass;
    }
}

the ++ operator takes a MyClass and returns an updated MyClass . ++运算符采用MyClass并返回更新的MyClass

Your ++ operator needs to take a Priority object and return an updated Priority object, what gets updated is up to you. 您的++运算符需要获取一个Priority对象并返回一个更新的Priority对象,更新的内容取决于您。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM