# Passing & Returning Objects to Functions

I have been giving a function by my instructor to be used to test if my code can handle a constant:

Distance fun (const Distance &x) {
Distance y, z;
cout << "fun:  passed value: ";
x.Paint ('M');
y = z = x;
y.SetDistance (1234.);
cout << "fun:  alterd value: ";
y.Paint ('M');
return z;
}

Also given by the instructor, is the test passing and return objects to functions:
a = fun (c);
cout << "a should now be the same as c: \n";
a.Paint ('M');
c.Paint ('M');

I continue to get a single error message: 'fun': identifier not found

class Distance {
/*==================================*/
/* class data
/*==================================*/
protected:
double totalmm; //total distance in millimeters
/*==================================*/
/* class function
/*==================================*/
public:
//constructors
Distance ();
Distance (long millimeter);
Distance (long meter, long centimeter, long millimeter);
Distance (int feet, int inches);
Distance (double miles);

//Access Functions
double GetDistance () const;  // distance storing converted to mm
void SetDistance (double mm); // set the stored distance accordingly

//Conversion functions
double MtoE () const;
double EtoM (int feet, int inches) const;  // one to pass 2 ints, & one to pass a double
double EtoM (double miles) const;

//Determine how to print the distance
void Paint (char unit) const; // single char

//Fun Fuction
Distance fun (const Distance &x);

};
My implementation file:
#include <iostream>
#include <iomanip>
using namespace std;

#include "Distance.h"

/*==============================================*/
/* Distance:  default constructor, no parameters
/*      use 0 as the distance??
/*==============================================*/
Distance::Distance() {
totalmm = 0; //using 0 as distance
}

//==============================================//
//GetDistance:  returns a double which is the
//  distance I am storing converted to mm
//==============================================//
double Distance::GetDistance () const {
}
//==============================================//
//SetDistance:  accepts a double mm - set stored
//   distance accordingly
//==============================================//
void Distance::SetDistance(double mm) {
totalmm = mm;
}

/*==================================*/
/* Distance:  constructors
/*==================================*/
//Accept long number of millimeters
Distance::Distance (long mm) { }

//Accept trio of longs - meters, centimeters, & millimeters
Distance::Distance (long m, long cm, long mm) {
}

//Accept 2 ints - feet & inches
Distance::Distance (int ft, int in) {
totalmm = EtoM(ft,in);
}

//Accept double miles
Distance::Distance (double mi) { }

//==============================================//
//Metric to English (MtoE): returns double representing
// the feet that this distance represents
//=============================================//
double Distance::MtoE () const {
double result;
result = (totalmm * MTF) / 1000;
return result;
}

//=============================================//
//English to Metric (EtoM):  two ints (feet and inches),
// and returns the double millimeters
//============================================//
double Distance::EtoM (int feet, int inches) const {
double result;

// (feet * 12 + inches) / (12 * 25.4)
result = (feet * I + inches) ;// I * ITM;
return result;
}

//============================================//
//English to Metric (EtoM): double miles and returns
// the double millimeters
//============================================//
double Distance::EtoM (double miles) const {

double result;
// miles * 160934
result = totalmm * MTM;
return result;
}

//============================================//
//Paint: char which is sed to determine how to print
// distance
//===========================================//
void Distance::Paint (char unit) const {

if (toupper (unit) == 'M')
cout << "Distance is = " << totalmm << " millimeters \n";
if (toupper (unit) == 'F')
cout << "Distance is = " << totalmm  << " feet \n";
if (toupper (unit) == 'I')
cout << "Distance is = " << totalmm  << " meters and millimeters \n";
if (toupper (unit) == 'L')
cout << "Distance is = " << totalmm << " miles \n";
}

//============================================//
//Fun:
//
//===========================================//

Distance fun (const Distance &x) {
Distance y, z;
cout << "fun:  passed value: ";
x.Paint ('M');
y = z = x;
y.SetDistance (1234.);
cout << "fun:  alterd value: ";
y.Paint ('M');
return z;
}

And finally my test program... towards the bottom is where the compiler is pointing to my error.
#include <iostream>
#include <iomanip>

#include "Distance.h"

using namespace std;

int main () {

//==========================================//
// Create a main program to test the Distance class//
//==========================================//
cout << "Testing Distance Class";

Distance a;
Distance b (1000.);
Distance c (100L, 50L, 8L);
Distance d (10 , 11);
Distance f (1.23);

a.SetDistance (1234.);
cout << "Testing GetDistance for object a - 1234 mm = "
<< a.GetDistance () << " mm\n\n";
cout << "Testing MtoE member function 1234 mm equals "
<< a.MtoE () << " feet\n\n";

//And you still aren't storing any data (*hint*)
//storing the variables passed in in their converted form...
int feet = 10;
int inches = 11;
d.EtoM (10, 11);
cout << "Testing EtoM member function 10 feet, 11 inches equals "
<< d.EtoM (feet, inches) << " millimeters\n\n";

double miles = 0;
f.SetDistance (1.23);
cout << "Testing EtoM member function 1.23 miles equals "
<< f.EtoM (miles) << " millimeters\n\n";

cout << " _______________________________________\n\n";
//========================================//
//Now use these 5 objects and test the Paint function ???
//========================================//

b.SetDistance (1000.);
d.Paint  ('M');  //  = 131 millimeters
b.Paint ('f') ;  //  = 1000 feet
f.Paint ('l');  // = 1.23 miles
cout << endl << endl;

cout << " _______________________________________\n\n";
//========================================//
//Next test assignment                                      //
//========================================//

a = b;
cout << "a should now be the same as b: \n";
a.Paint ('M');
b.Paint ('M');

cout << " _______________________________________\n\n";

//========================================//
//Test passing & returning objects to functions //
//========================================//

cout << "a should now be the same as c: \n";
a.Paint ('M');
c.Paint ('M');

return 0;
}
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Put the fun function in the same file as the main (above the main).
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Senior Software Engineer (Avast)Commented:
You have fun declared as a member function of Distance but you've defined it as a free standing function. Try changing it to like below.
``````Distance Distance::fun (const Distance &x) {
Distance y, z;
cout << "fun:  passed value: ";
x.Paint ('M');
y = z = x;
y.SetDistance (1234.);
cout << "fun:  alterd value: ";
y.Paint ('M');
return z;
}
``````
0
Commented:
and it should NOT be a member function of Distance, as it is a test function - ie. a function with the sole purpose to test the Distance class.
0
Senior Software Engineer (Avast)Commented:
>> and it should NOT be a member function of Distance, as it is a test function - ie. a function with the sole purpose to test the Distance class.
Well, it could be a self test function. It will need to be declared static though if that's how you want to use it.

class Distance {
public:
...
static Distance fun (const Distance &x);
...
};
``````class Distance {
public:
...
static Distance fun (const Distance &x); // test function
...
};

Distance::fun(x);
``````
0
Commented:
>> Well, it could be a self test function.

I don't think that was intended ;)

Note that the teacher said to call the function like this :

a = fun (c);

Just move the fun function to the same file as the main (above the main), and remove these lines from the Distance class :

//Fun Fuction
Distance fun (const Distance &x);
0
Commented:
As 'fun()'' is also declared as a class member, there's another issue - wherever you put it, you need to remove the declaration of fun

//Fun Fuction
Distance fun (const Distance &x);

from inside 'Distance' - make that

``````
#include <iostream>
#include <iomanip>

#include "Distance.h"

// Fun Function
Distance fun (const Distance &x); // <----

using namespace std;

int main () {
//...
}
``````
0
Commented:
I think that was pretty much what I said, jkr ;)
0
Author Commented:
Thanks Infinity08!!
And thanks to the rest, but Infinity's answer is the answer I was looking for....
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.