Taking adress of temporary

I need to return a value from a list to construct one object:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Vertice::Vertice(int ID)
{
	id = ID;
	P = &(regresaPunto(id));
}

Punto regresaPunto(int n)
{
	list<Punto> aux;
	list<Punto>::iterator j;
	//Punto *pt;

	int i = 1;
	aux = pun;       // Auxiliar list
	j = aux.begin();

	advance(j, n);

	//pt = &(*j);
	return *j;
}


But when I compile, it sends me the next error:

Taking adress of temporary [-fpermissive]

Can you help me? I can't use std::vector so that's out of the question
Last edited on
Next time you post a compiler error, can you please post the full error and indicate what line the error is on. Just because the error have information you don't understand, doesn't mean it's irrelevant.
Fortunately, in this case, the error is obvious from what you've posted.

But before you do that, you create a local container in function regresaPunto. Then you create an iterator that points to somewhere in the collection. Then you return the address of that iterator, knowing that the container and the iterator will be destroyed when that function exits.

The value the function returns points to an object that no longer exists, right?

You have to post a bit more code. What is P, and how is it used? What information do you want out of regresaPunto?
Here's the full code of the Vertice

1
2
3
4
5
6
7
8
9
10
class Vertice
{	
	public:
		Vertice(int ID);
		int id;
		Vertice *siguiente;
		Punto *P;

	friend class Figura;
};


As you can see, P is a pointer to a class Punto

1
2
3
4
5
6
7
8
9
10
11
12
class Punto
{
	public:
		Punto(float a, float b, float c);
		Punto();
		float x;
		float y;
		float z;
		Punto *siguiente;
	
	friend class Figura;
};
If it's a pointer to a Punto, where are all these Puntos to be pointed to?
Last edited on
Yes, since Punto is another class with its own data
Yes, since Punto is another class with its own data
I can see that, but if you have a pointer, it must point to an object (or point to nothing, in which case the value is nullptr).
why dont you just create a factory class for Points that will store these allocations into list? seems simpler to me.

Also provide translation for spanish , I dont have the time to translate your stuff. thks.
Topic archived. No new replies allowed.