?
Solved

Trace a path of an array

Posted on 2008-02-10
3
Medium Priority
?
216 Views
Last Modified: 2011-09-20
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

0
Comment
Question by:gndcdosps
3 Comments
 
LVL 11

Expert Comment

by:Deepu Abraham
ID: 20862920
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
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 20862951
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
 

Author Closing Comment

by:gndcdosps
ID: 31429677
Thanks jkr... Your suggestion worked perfectly!!!!
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question