Let's consider the following code. You have a class that holds data and a list of elements of the other class, which holds its own data and a pointer of the first type to map to. It would be schematically like this:
struct nodo
{
dato datonodo;
std::list<arista>ListaAristas;
};
struct arista
{
dato2 datoarista;
nodo* destino;
};
Finally I insert these nodes in a list:
std::list<nodo>lista;
Well, if I directly access a node, and within it its EdgeList, I have no problems, but if I iterate over the list of nodes, and then, within each element of the list, I try to access its EdgeList, i get nothing.
This would be an example:
#include <iostream>
#include <list>
struct nombre;
struct relacion
{
std::string datorelacion;
nombre* destino;
relacion(std::string dato):datorelacion(dato) {}
relacion(const relacion& otro):datorelacion(otro.datorelacion)
{
destino = otro.destino;
}
relacion operator=(const relacion& otro)
{
if (this!=&otro)
{
datorelacion=otro.datorelacion;
destino=otro.destino;
return *this;
}
}
};
struct nombre
{
//miembros
std::string datonombre;
std::list<relacion>ListaRelaciones;
//metodos
nombre (std::string dato):datonombre(dato) {}
nombre (const nombre& otro):datonombre(otro.datonombre), ListaRelaciones(otro.ListaRelaciones){}
nombre& operator=(const nombre&otro)
{
if (this!=&otro)
{
datonombre = otro.datonombre;
ListaRelaciones = otro.ListaRelaciones;
return *this;
}
}
};
int main()
{
std::string tipo1="es amigo de ";
std::string tipo2="es enemigo de ";
std::string tipo3="es familar de ";
/***********************************/
std::list<nombre>listanombres;
/**********************************/
nombre N1("Pepe");
listanombres.push_back(N1);
/********************************/
nombre N2("Juan");
listanombres.push_back(N2);
/******************************/
nombre N3("Ana");
listanombres.push_back(N3);
/******************************/
nombre N4("Pepa");
listanombres.push_back(N4);
/********RELACIONES************/
relacion R1(tipo1);
relacion R2(tipo2);
relacion R3(tipo3);
R1.destino = &N2;
R2.destino = &N3;
R3.destino = &N4;
N1.ListaRelaciones.push_back(R1);
N1.ListaRelaciones.push_back(R2);
N2.ListaRelaciones.push_back(R3);
N3.ListaRelaciones.push_back(R3);
/******accedo a un nodo cualquiera de forma directa-->FUNCIONA***********/
for (const auto& elem : N1.ListaRelaciones)
{
std::cout<<N1.datonombre<<" "<<elem.datorelacion<<" "<<elem.destino->datonombre<<"\n";
}
/******accedo a cada elemento de la lista, y luego intento acceder a la ListaArista de cada uno de ellos--->NO FUNCIONA************/
for (const auto& elem : listanombres)
{
std::cout<<elem.datonombre<<" ";
for (auto elem2 : elem.ListaRelaciones)
{
std::cout<<elem2.datorelacion<<" "<<elem2.destino->datonombre;
}
std::cout<<"\n";
}
return 0;
}
It is normal that it does not work because you are modifying the originals, not the copies that you add to your lists.
When you add elements of type
nombre
tolistanombres
(N1
,N2
,N3
andN4
) you are copying the elements, when you later alter the namesN1
,N2
andN3
adding relations to them you are not altering the copies withinlistanombres
but the originals. I have corrected and simplified your code:The most relevant changes are:
= default
to the end of their declaration. You need to explicitly ask the compiler to do this because creating a constructor disables the other constructors generated by default.