Trace a path of an array

I need help determining how to incorporate an array from a class into a function.

I have a function called 'AddPath' and then I have a function called 'ShowPath'.  
My problem resides in 'ShowPath" and how to get the series of messages called in main from the 'AddPath' function to display from the 'ShowPath" function.

Desired Output:
Tracing this path of 3 segments
Segment 1: 45 mm
Segment 2: 105 mm
Segment 3: 9 mm

//The main tester program
         Distance d [3];
	d[0].SetDistance (Distance::Metric, 45);
	d[1].SetDistance (Distance::Metric, 105);
	d[2].SetDistance (Distance::Metric, 9);
	{//allow destructor to be called before main ends
		Path p;
		p.AddPath (d[0]);
		p.AddPath (d[1]);
		p.AddPath (d[2]);
		p.ShowPath ();
	}
// Path class - including AddPath and ShowPath function
#include <iostream>
#include <iomanip>
using namespace std;
 
#include "Path.h"
/*==============================================*/
/*Path:  
/*==============================================*/
Path::Path () {
	path = 0;
	count = 0;
}
/*==============================================*/
/* ~Path:  remove allocated path array  
/*==============================================*/
Path::~Path () {
	//for (int i=count-1; i >= 0; i--) delete path [i];
	if (path) delete [] path;
	path = 0;
	count = 0;
}
/*==============================================*/
/* AddPath:  basically a growable array type of data
/* structure
/*==============================================*/
void Path::AddPath (Distance &d) {
Distance* temp = new Distance [count +1]; // allocate an array
if (!temp) {
	cerr << "Array:  AddPath error - out of memery \n";
	return;
	}
for (int i = 0; i < count; i++) {
	temp [i] = path [i];
	}
temp [count] = d;
count ++;
if (path) delete [] path;
path = temp;
}
 
/*==============================================*/
/* ShowPath: does not take any parameters.  Should
/* display series of message that trace the path array
/* of distance.
/*==============================================*/
void Path::ShowPath ( ) {
	cout << "Tracing this path of " << count << " segments. \n";
	for (int i = 0; i < count; i++) {
		cout << "Segment " << i + 1 <<  ": ";			
	cout << path [i] -> AddPath ();  // getting errors here
	}
}

Open in new window

gndcdospsAsked:
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.

Deepu AbrahamR & D Engineering ManagerCommented:
cout << path [i] -> AddPath ();  // getting errors here
 You may have to write a function  called GetPath() which actually returns the path information. AddPath() is not returning any thing its a void function.

Best Regards,
DeepuAbrahamK
0
jkrCommented:
Since you aren't storing pointers but instances, that should be

void Path::ShowPath ( ) {
        cout << "Tracing this path of " << count << " segments. \n";
        for (int i = 0; i < count; i++) {
                cout << "Segment " << i + 1 <<  ": ";                  
        cout << path [i].AddPath ();  // no ->, but a '.'
        }
}

But anyway, this is not what you want to do - that should be more like
void Path::ShowPath ( ) {
        cout << "Tracing this path of " << count << " segments. \n";
        for (int i = 0; i < count; i++) {
                cout << "Segment " << i + 1 <<  ": ";                   
        cout << path [i].GetDistance(Distance::Metric) << " mm" << endl;
        }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

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

Start your 7-day free trial
gndcdospsAuthor Commented:
Thanks jkr... Your suggestion worked perfectly!!!!
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.