3 Replies - 919 Views - Last Post: 17 June 2012 - 12:42 PM Rate Topic: -----

#1 killermunk1   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 05-February 12

Accessing an array that was passed by reference

Posted 17 June 2012 - 11:39 AM

I am having an issue that I do not think I have ran into before. I am creating a Dijkstra function for my Graph ADT. I need to return two arrays so I passed them by reference. However it will not let me access them. I am thinking that it is just a syntax issue, but I am not sure how to fix it. Also as a side note, how would I make the priority queue work as a minimum queue.

the error is Error: expression must have pointer to object type, this applies to every time I try to access dist or prev.

void GraphType::Dijkstra(string vertex, int& dist, int& prev)
{
	
	for(int i = 0; i< numVertices; i++)//set dist and prev to infinite and undefined respectivly
	{
		dist[i] = INT_MAX;
		prev[i] = -1;
	}
	int start = IndexIs(vertices, vertex); //get the index of the starting vertex
	dist[start] = 0;
	MarkVertex(vertex);//mark said starting vertex

	priority_queue<int> Q;//set up priority queue

	for(int i = 0; i<numVertices; i++)//for loop to add the vertices not vistited yet
	{
		if(marks[i] == false)
			Q.push(i);
	}
	int u, newdist;
	queue<int> while_queue;
	while(Q.size() != 0)
	{
		u = Q.top();
		MarkVertex( u);
		GetToVertices(u, while_queue);
		for(int v = 0; v<while_queue.size(); v++)
		{
			newdist = dist[u]+WeightIs(u,v);
			if(newdist < dist[v])
			{
				dist[v] = newdist;
				prev[v] = u;
				Q.push(dist[v];
			}
		}

	}
	//ClearMarks();

}


This post has been edited by killermunk1: 17 June 2012 - 11:43 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Accessing an array that was passed by reference

#2 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Accessing an array that was passed by reference

Posted 17 June 2012 - 11:48 AM

You can't pass an array by reference. You pass the array and it decays to a pointer.
#include <iostream>

void output_array(int ar[], int sz) {
		for (int i = 0; i < sz; i++)
		{
			std::cout << ar[i] << std::endl;
		}
	ar[4] = 42;
}

int main (int argc, char * const argv[]) {
	
	int myarray[5] = {99,98,97,96,95};
	
	output_array(myarray, 5);
	
	std::cout << "See that the function changed the last value: " <<  myarray[4] << std::endl << std::endl;
	output_array(myarray, 5);
	return 0;
}


This post has been edited by CTphpnwb: 17 June 2012 - 11:52 AM

Was This Post Helpful? 0
  • +
  • -

#3 killermunk1   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 05-February 12

Re: Accessing an array that was passed by reference

Posted 17 June 2012 - 12:10 PM

hmm, then what should I do just pass the arrays normally? If I do do that would it still change the dist and prev arrays that were passed in my main function?

edit: or maybe I could make dist and prev a struct then pass the struct in by reference?

This post has been edited by killermunk1: 17 June 2012 - 12:11 PM

Was This Post Helpful? 0
  • +
  • -

#4 killermunk1   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 05-February 12

Re: Accessing an array that was passed by reference

Posted 17 June 2012 - 12:42 PM

I think I have it working now, I still need to set up the minimum priority queue though. I did what I had said above and took the two arrays and made them a struct.

void GraphType::Dijkstra(string vertex, dijkstra & dist_prev)
{
	
	for(int i = 0; i< numVertices; i++)//set dist and prev to infinite and undefined respectivly
	{
		dist_prev.dist[i] = INT_MAX;
		dist_prev.prev[i] = -1;
	}
	int start = IndexIs(vertices, vertex); //get the index of the starting vertex
	dist_prev.dist[start] = 0;
	MarkVertex(vertex);//mark said starting vertex

	priority_queue<int> Q;//set up priority queue

	for(int i = 0; i<numVertices; i++)//for loop to add the vertices not vistited yet
	{
		if(marks[i] == false)
			Q.push(i);
	}
	int u, newdist;
	queue<int> while_queue;
	while(Q.size() != 0)
	{
		u = Q.top();
		MarkVertex( u);
		GetToVertices(u, while_queue);
		for(int v = 0; v<while_queue.size(); v++)
		{
			newdist = dist_prev.dist[u]+WeightIs(u,v);
			if(newdist < dist_prev.dist[v])
			{
				dist_prev.dist[v] = newdist;
				dist_prev.prev[v] = u;
				Q.push(dist_prev.dist[v]);
			}
		}

	}
	//ClearMarks();

}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1