Something funny happening with operator overloading

This is a discussion on Something funny happening with operator overloading within the C++ Programming forums, part of the General Programming Boards category; The arithmetics of the program are incorrect mvector.h Code: #include <cmath> #ifndef _MVECTOR_H #define _MVECTOR_H namespace math { class Vector ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    17

    Something funny happening with operator overloading

    The arithmetics of the program are incorrect
    mvector.h

    Code:
    #include <cmath>
    #ifndef _MVECTOR_H
    #define _MVECTOR_H
    namespace math
    {
    class Vector
    {
        public:
            float x;
            float y;
            float z;
    
            Vector(float a=0,float b=0,float c=0)
            {
                x=a;
                y=b;
                z=c;
            }
    
    
        Vector operator+(Vector &v1)
        {
            Vector tmp;
               tmp.x=v1.x+x;
            tmp.y=v1.y+y;
            tmp.z=v1.z+z;
                return tmp;
        };
    
        Vector operator-(Vector v1)
        {
            Vector tmp;
            tmp.x=x-v1.x;
            tmp.y=y-v1.y;
            tmp.y=z-v1.z;
    
                return tmp;
        };
    
    
    float VecLength(Vector v)
    {
        return sqrt(pow(v.x,2)+pow(v.y,2)+pow(v.z,2));
    };
    
    float DotProduct(Vector v1,Vector v2)
    {
        return (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
    }
        Vector CrossProduct(Vector v1, Vector v2)
        {
            Vector temp;
            temp.x=v1.y*v2.z - v1.z*v2.y;
            temp.y=v1.z*v2.x - v1.x*v2.z;
            temp.z=v1.x*v2.y - v1.y*v2.x;
            return temp;
        };
    };
    }
    #endif




    main.cpp
    Code:
    #include <iostream>
    #include <cmath>
    #include "Mvector.h"
    using namespace math;
    using namespace std;
    Vector a= Vector(1,-8,-3);
    Vector b=Vector(2,2,3);
    
    Vector c= a-b;
    
    int main()
    {
    	std::cout <<c.x<< std::endl;
    	std::cout <<c.y<< std::endl;
    	std::cout <<c.z<< std::endl;
    	return 0;
    }
    for some reason it outputs:
    -1
    -6
    0
    whats wrong?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Code:
            tmp.x=x-v1.x;
            tmp.y=y-v1.y;
            tmp.y=z-v1.z;
    There's an error here, but I'm not telling you what.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    17
    Oops! Thanks for pointing that one out!

  4. #4
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    Your class design is not very object oriented. A vector object should be able to calculate its own length, there doesn't have to be a function to do that for them, if you know what I mean

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,287
    Quote Originally Posted by pronecracker View Post
    Your class design is not very object oriented. A vector object should be able to calculate its own length, there doesn't have to be a function to do that for them, if you know what I mean
    This is a common fallacy.

    "Object oriented" is not the same thing as "all functions to calculate some property of an object must be member functions of the class".

    For discussion of cases, in C++, where implementing operations as member functions can actually reduce benefits associated with encapsulation of a class, have a look here.
    Last edited by grumpy; 04-14-2007 at 07:22 AM.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    17
    So instead of v.VecLength(v) i should have v.VecLength?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    No, not at all. What grumpy is saying is that just because a free function is used instead of a member function does not mean the code is any less object oriented.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by grumpy View Post
    This is a common fallacy.

    "Object oriented" is not the same thing as "all functions to calculate some property of an object must be member functions of the class".

    For discussion of cases, in C++, where implementing operations as member functions can actually reduce benefits associated with encapsulation of a class, have a look here.
    eh?

    I don't disagree that it's sometimes more practical to do things that way but it is not, by definition, OOP. it's unfortunate that C++ doesn't allow us to define non-member functions and yet have the option to call them as members, ie:

    Code:
    void
    bar(foo const &);
     
    foo foo;
    bar(foo);
    foo.bar();
    that would allow you to do a lot of neat things, if you think about it.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,287
    Quote Originally Posted by Sebastiani View Post
    I don't disagree that it's sometimes more practical to do things that way but it is not, by definition, OOP.
    I'd be very interested to see an accepted definition of OOP that specifically requires that all operations on a class be members of a class.

    Quote Originally Posted by Sebastiani View Post
    it's unfortunate that C++ doesn't allow us to define non-member functions and yet have the option to call them as members, ie:

    Code:
    void
    bar(foo const &);
     
    foo foo;
    bar(foo);
    foo.bar();
    that would allow you to do a lot of neat things, if you think about it.
    If you really need to do that, provide both the member and non-member versions, and have one call the other.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Funny Windows Error Codes (I rofled...)
    By Sentral in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 06-01-2006, 09:38 AM
  2. Funny things happening to values outside of function
    By drb2k2 in forum Windows Programming
    Replies: 2
    Last Post: 04-10-2003, 02:39 PM
  3. Funny things happening outside of function
    By drb2k2 in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2003, 02:26 PM
  4. Absolutely mind bogglingly funny
    By Imperito in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-06-2002, 09:23 PM
  5. Funny
    By Imperito in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 08-21-2002, 05:41 PM

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