Topic: Student needing help with C++ function overloading???
Author: jk@zarniwoop.pc-labor.uni-bremen.de (Jens Kuespert)
Date: 1995/04/24 Raw View
>>>>> On 21 Apr 1995 17:54:55 GMT, mshafiq@aol.com said:
mshafiq> Can someone please help!
I wouldn't expect *anybody* to help you in this group. At least in my
understanding this group is for discussion of standard issues. Maybe
you should try comp.lang.c++ or something like that.
--
-- Jens --
http://wowbagger.pc-labor.uni-bremen.de/jens.html
Author: glascock@esd.dl.nec.com (Trent Glascock)
Date: 1995/04/25 Raw View
In article <3nb582$l1v@warp.cris.com>, mshafiq@aol.comORshafiq@cris.com says...
>This gives me an error :
>member 'number::val' of class 'number' is not accessible.
>Obviously because 'val' can only be accessed by member functions of class
>number! So my dilemma is, how can I get an overloaded sqrt() function to
>take and return parameters of type number?
Make the overloaded function a *friend*.
--
Trent Glascock
glascock@esd.dl.nec.com
Author: Mohammed Shafiq <mshafiq@aol.com OR shafiq@cris.com>
Date: 1995/04/22 Raw View
Thankyou for your suggestion(s).
However, I am still slightly confused as to how would the sqrt function
then knows to access the private variable stored inside the number
class?
I tried making the function sqrt() a non-member function, thus
..
number sqrt(number &n2)
{
number temp;
temp.val = sqrt(n2.val);
return temp;
}
This gives me an error :
member 'number::val' of class 'number' is not accessible.
Obviously because 'val' can only be accessed by member functions of class
number! So my dilemma is, how can I get an overloaded sqrt() function to
take and return parameters of type number?
Author: mshafiq@aol.com
Date: 1995/04/21 Raw View
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!
Author: mshafiq@aol.com
Date: 1995/04/21 Raw View
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!
Author: mshafiq@aol.com
Date: 1995/04/21 Raw View
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!
Author: mshafiq@aol.com
Date: 1995/04/21 Raw View
hello baby
love you
xxxoxxx
Author: b91926@fsgm01.fnal.gov (David Sachs)
Date: 1995/04/21 Raw View
mshafiq@aol.com writes:
>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
You are declaring number::sqrt(number) as a member function, when
your usage indicates that you really want a non-member function.
Simply remove the sqrt declaration from the number class and declare
the function:
number sqrt(number n2) { ... }
If necessary, declare this function as a friend of class number.
Author: jimk@lysander.wx.ll.mit.edu ( James Knowles )
Date: 1995/04/21 Raw View
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
---------------------------------