Overriding = operator.

This is a discussion on Overriding = operator. within the C++ Programming forums, part of the General Programming Boards category; I am working on a project using templates. I have a template class called Measures and another regular class called ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    7

    Overriding = operator.

    I am working on a project using templates. I have a template class called Measures and another regular class called Meters.
    I overrode the + operator in Measures and now want to use that
    function when I add two Meters together. However, all I get is an error saying error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class Meters' (or there is no acceptable conversion).
    I can't figure out what I'm doing wrong.
    Here's the code for the = operator, the + operator, and main.

    // Overload = operator.
    template <class T>
    Measure<T>& Measure<T>:perator=(const Measure &rhs)
    {
    if (this == &rhs)
    {
    return *this;
    }
    this.measure = rhs.getMeasure();
    return *this;
    }

    // Overload + operator.
    template <class T>
    T& Measure<T>:perator+(Measure &s1)
    {
    double v1, v3;
    v1 = s1.getMeasure();
    v3 = v1 + s1.measure;
    Measure s3(v3);
    return s3;
    }

    int main()
    {
    double x = 2.0;
    int y = 3;
    Meters m1(5.0);

    Measure<Meters> theMeasure(4.0);
    Measure<Meters> anotherMeasure(3.0);
    Measure<Meters> finalMeasure(0.0);
    finalMeasure = theMeasure + anotherMeasure;
    cout << finalMeasure.getMeasure();

    return 0;
    }

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Have you overloaded the = operator in the Meters class?
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    7
    I don't know how, but I figured it out. Its working now.
    Thanks anyway.

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    What was wrong with it?
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Do not return a reference where you must return an object....

    operator + should return an object and not a reference. At present you are returning a reference to an object that has gone out of scope.....
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 07:38 AM
  4. Overriding '+' operator
    By cunnus88 in forum C++ Programming
    Replies: 6
    Last Post: 11-14-2005, 03:24 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21