tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
2
ZUGRIFFE
771
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    d_Artagne d_Artagne ist offline Mitglied Bronze
    Registriert seit
    Mar 2005
    Beiträge
    29
    Hi,


    Ich hab mal versucht einen Operator zu überladen. Ich krieg immer
    dieselbe Fehlermeldung, aber ich kann mir beim besten Willen nicht
    erklären warum. Hier der Code:
    Code :
    1
    2
    3
    4
    5
    6
    7
    
    public static bool operator > (DateTime d1, DateTime d2)
            {
                if (d1.Year > d2.Year) { return true; }
                else if (d1.Month > d2.Month) { return true; }
                else if (d1.Day > d2.Day) { return true; }
                return false;
            }

    Die Fehlermeldung:

    One of the parameters of a binary operator must be the containing type
     

  2. #2
    TommyMo TommyMo ist offline Mitglied Brokat
    Registriert seit
    Nov 2003
    Beiträge
    261
    Hi!

    Könntest du mal deine ganze Klasse anführen? Hätte eine Vermutung, bin mir aber nicht sicher.

    Gruß
    TOM
     
    alles Gute kommt von ...

  3. #3
    Avatar von Norbert Eder
    Norbert Eder Norbert Eder ist offline Mitglied Diamant
    Registriert seit
    Feb 2004
    Ort
    Österreich / Graz
    Beiträge
    5.137
    Blog-Einträge
    51
    Hab da einen Text für dich, der dir helfen sollte. Betrifft zwar den + Operator, aber im Endeffekt eh das gleiche. Ach ja, was mir nur aufgefallen ist. Wenn du > überladest, musst du auch < überladen.

    In the operator plus, we are creating a new object whose variable i is the summation of the i's of the objects a and b. The normal plus operator works as usual. We normally overrule the predefined string operator that always gets called whenever our user defined type has to be converted to a string. This is needed to get a confirmation that the plus operator actually did the job it was supposed to do.

    Code :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    
    public class zzz {
     
      public static void Main()
      {
      }
     
    }
     
    class yyy {
     
      public static yyy operator + ( xxx a, xxx b)
      {
        return new yyy();
      }
     
      public static yyy operator ++ ( xxx a)
      {
        return new yyy();
      }
    }
     
    class xxx
    {
    }
    Compiler Error
    a.cs(7,19): error CS0563: One of the parameters of a binary operator must be the containing type
    a.cs(11,19): error CS0559: The parameter and return type for ++ or -- operator must be the containing type


    Whenever an operator is overloaded, one of the parameters to the function must be the data type of the class that contains the operator. In this case, while overloading the binary operator +, at the most one of its parameters must be of type yyy. If not, then it is assumed to be redefining an operator of another class. In the above case, the code of the operator + belongs to class xxx and not yyy.

    In the case of a unary operator, it is evident that the only parameter must be the type of the class. In case you have forgotten, the only ternary operator cannot be overloaded. By failing the above rules, you can never supersede an existing predefined operator definition.

    The cast operators are resolved using the same principles but by using type names instead of operator symbols. Element access like an array is achieved using indexers that has absolutely has no relationship at all with operator overloads.
    Geändert von Norbert Eder (11.10.05 um 17:20 Uhr)
     

Ähnliche Themen

  1. Überladen von Operatoren
    Von LukeS im Forum C/C++
    Antworten: 2
    Letzter Beitrag: 24.07.08, 23:04
  2. Überladen in C++
    Von fox_2_k im Forum C/C++
    Antworten: 4
    Letzter Beitrag: 15.06.08, 01:31
  3. Operatoren
    Von TWurst im Forum C/C++
    Antworten: 2
    Letzter Beitrag: 08.08.07, 17:00
  4. Antworten: 1
    Letzter Beitrag: 20.02.07, 14:10
  5. Überladen von Operatoren
    Von Java/CppProgrammer im Forum Java
    Antworten: 1
    Letzter Beitrag: 19.10.04, 14:17