I'm having trouble with passing a linked list between functions. I did some research online and found a few things, one of which was here, that led to the conclusion that I am not passing my linked list by reference. However, I have tried numerous different syntaxes to do this, and nothing seems to work. If I add an ampersand to the list I'm trying to pass, I get this error:

"error: invalid initialization of non-const reference of type 'list&' from a temporary of type 'list*'"

Below is a vastly simplified and shorter piece of code that replicates the problem. I can confirm that the program crashes with a segfault when the call to Test2 is made, but not if it is commented out. In addition, after the call to Test2, the contents of tester are printed as After Test2, tester = [0.000000, 0.000000; 0.000000, 0.000000]

static void Test1()
{
	// Variable declarations
	list tester;
	double tempArray[2];

	// Create a linked list to test with
	for (int i = 0; i<10; i++)
	{
		tester.append(i, 2*i);
	}

	// Print the list for test output
	printf("After creation, tester = [");
	for (int i = 0; i<(tester.count()-1); i++)
	{
		tempArray[0] = tester.getx(i);
		tempArray[1] = tester.gety(i);
		printf("%f, %f; ", tempArray[0], tempArray[1]);
	}
	tempArray[0] = tester.getx(tester.count()-1);
	tempArray[1] = tester.gety(tester.count()-1);
	printf("%f, %f]\n", tempArray[0], tempArray[1]);

	// Pass the list to another function, hopefully by reference
	//Test2(tester);

	// Print the list again for comparison
	printf("After Test2, tester = [");
	for (int i = 0; i<(tester.count()-1); i++)
	{
		tempArray[0] = tester.getx(i);
		tempArray[1] = tester.gety(i);
		printf("%f, %f; ", tempArray[0], tempArray[1]);
	}
	tempArray[0] = tester.getx(tester.count()-1);
	tempArray[1] = tester.gety(tester.count()-1);
	printf("%f, %f]\n", tempArray[0], tempArray[1]);

}

static void Test2(list tester)
{
	// Variable declarations
	double x, y;
	int int_x;

	// Go through the list, and remove every element for which the first term
	//		in the array is even
	for (int i = 0; i<(tester.count()-1); i++)
	{
		x = tester.getx(i);
		y = tester.gety(i);

		int_x = (static_cast<int> (x));

		if ((int_x%2)==0)
		{
			tester.del(x, y);
		}
	}
}

In addition, here's the code from the linked list class that I wrote, in case that's where the problem is. However, I stole most of it from online, so I doubt that's the case.

class list
{
     private:

         struct node
         {
            double xPoint;
	    double yPoint;
            node *link;
         }*p;

     public:

         list();
         void append( double xNew, double yNew );
         void del( double xDel, double yDel );
	 double getx( int index );
	 double gety( int index );
         int count();
         ~list();
};

list::list()
{
     p=NULL;
}

void list::append(double xNew, double yNew)
{
   node *q,*t;

   if( p == NULL )
   {
      p = new node;
      p->xPoint = xNew;
      p->yPoint = yNew;
      p->link = NULL;
   }
   else
   {
      q = p;
      while( q->link != NULL )
           q = q->link;
      t = new node;
      t->xPoint = xNew;
      t->yPoint = yNew;
      t->link = NULL;
      q->link = t;
   }
}

void list::del( double xDel, double yDel )
{
   node *q,*r;
   q = p;
   if(( q->xPoint == xDel ) && ( q->yPoint == yDel ))
   {
      p = q->link;
      delete q;
      return;
   }

   r = q;
   while( q!=NULL )
   {
      if(( q->xPoint == xDel ) && ( q->yPoint == yDel ))
      {
         r->link = q->link;
         delete q;
         return;
      }

      r = q;
      q = q->link;
   }
}

double list::getx( int index )
{
	double ans;
	int count = 0;

	node *q;
	q = p;

	while( count < index )
	{
		q = q->link;
		count++;
	}

	ans = q->xPoint;

	return ans;
}

double list::gety( int index )
{
	double ans;
	int count = 0;

	node *q;
	q = p;

	while( count < index )
	{
		q = q->link;
		count++;
	}

	ans = q->yPoint;

	return ans;
}

int list::count()
{
   node *q;
   int c=0;
   for( q=p ; q != NULL ; q = q->link )
        c++;

   return c;
}

list::~list()
{
   node *q;
   if( p == NULL )
        return;

   while( p != NULL )
   {
        q = p->link;
      delete p;
      p = q;
   }
}

Hmm quick suggestion probably a crazy one :D does this work

static void Test2(list &tester)

Oh, okay. Yes, that worked. Could someone possibly explain to me why? I thought that the declaration had to be the same as the line at the top of the function, but this apparently works with the ampersand just on the latter and without one t the function call. Is this unique to pointers, or am I forgetting something about C++?

Sorry, I spoke too soon. I had a key line commented out while I was trying something myself. Your suggestion just changed the error message, so now I get:

error: 'void Test2(list)' used but never defined

Interestingly, the line number that is called out is the function declaration line at the top.

you need to add the & on the function declaration and definition

Chris

Ah, that's it. Thanks very much for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.