Solved

# Passing & Returning Objects to Functions

Posted on 2008-01-29
Medium Priority
1,238 Views
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;
}
0
Question by:gndcdosps

LVL 53

Accepted Solution

Infinity08 earned 2000 total points
ID: 20772884
Put the fun function in the same file as the main (above the main).
0

LVL 40

Expert Comment

ID: 20772887
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

LVL 53

Expert Comment

ID: 20772899
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

LVL 40

Expert Comment

ID: 20772920
>> 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

LVL 53

Expert Comment

ID: 20772950
>> 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

LVL 86

Expert Comment

ID: 20772983
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

LVL 53

Expert Comment

ID: 20772995
I think that was pretty much what I said, jkr ;)
0

Author Closing Comment

ID: 31426207
Thanks Infinity08!!
And thanks to the rest, but Infinity's answer is the answer I was looking for....
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
###### Suggested Courses
Course of the Month3 days, 3 hours left to enroll