From -7871801137687247268
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,24391b79cbddb10b
X-Google-Attributes: gidf78e5,public
From: jimk@lysander.wx.ll.mit.edu ( James Knowles )
Subject: Re: Student needing help with C++ function overloading???
Date: 1995/04/21
Message-ID: <JIMK.95Apr21165848@lysander.wx.ll.mit.edu>#1/1
X-Deja-AN: 101372552
references: <3n8rea$146@warp.cris.com>
organization: Massachvsetts Institvte of Technology
newsgroups: comp.std.c++

In article <3n8rea$146@warp.cris.com> mshafiq@aol.com writes:

   From: mshafiq@aol.com
   Newsgroups: comp.std.c++
   Date: 21 Apr 1995 17:53:14 GMT
   Organization: Concentric Research Corporation	

   I am a student of C++ trying to overload the sqrt() function so that it   
   takes and returns a self-defined object of type 'number'. 
   I have been assigned this exercise purely to demonstrate operator and   
   function overloading - the overloaded operators are working fine, but   
   when I try to call the overloaded sqrt() function thus: 

   CODE FRAGMENT BELOW 
   .. 

   class number { 
	   double val; 
   public: 
	   number(double d){val=d;}; 
	   number() {val=0.0;}; 
	   void get_number (double &d){d = val;}; 
	   void operator=(double d){val = d;}; 
	   number operator +(number n2); 
	   . 
	   . 
	   number sqrt(number n2);                                           
	   } 

   .. 
   number number::sqrt(number n2) 
   { 
     number temp; 
     temp.val= sqrt(n2.val); 
     return temp; 
   } 

   .. 
   .. 
   .. 
   n3=sqrt(n2); 


   where n2 and n3 are objects of type 'number' with a private variable of   
   type double, 
   I receive the error "cannot convert from number to a double" (Symantec   
   compiler). 
   Can someone please help!

-------------------------------------------------


    nowhere  do you have the means to create a 'number'

    from another number.  there are doubles everywhere,

    but the compiler will not juggle them all around to

    convert things for you


    ---- at the least you need an operator=, and a copy
         ctor that take 'number' as input

     the compiler will NOT :

         take a number.....make it into a double by inventing
         a function I have not defined myself.....
         pass that double to a 'number' ctor......
         make a temp 'number'....use it to redefine
         a pre-existing 'number'.....

       the ability to flip/flop between double and number
       exists only in YOUR perception.  You only told
       the compiler one side of the story....it won't make
       up the rest of the story for you

---------------------------------




