Error- taking address of temporary -free C ++

0
 #include <iostream>
 #include <set>

using namespace std;

void leerConjunto(set<int> *, int);

void imprimirConjunto(set<int> *);

set<int> opUnion(set<int> *, set<int> *);

set<int> opInterseccion(set<int> *, set<int> *);

set<int> opDiferencia(set<int> *, set<int> *);

int main() {
set<int> a, b;
int aux;

cout << "Cuantos elementos son en el conjunto A: ";
cin >> aux;

leerConjunto(&a, aux);

cout << endl << "Cuantos elementos son en el conjunto B: ";
cin >> aux;

leerConjunto(&b, aux);

cout << endl << "Conjunto A:";
imprimirConjunto(&a);

cout << "Conjunto B:";
imprimirConjunto(&b);

cout << "A union B:";
imprimirConjunto(&opUnion(&a, &b));

cout << "A interseccion B:";
imprimirConjunto(&opInterseccion(&a, &b));

cout << "A - B:";
imprimirConjunto(&opDiferencia(&a, &b));

cout << "B - A:";
imprimirConjunto(&opDiferencia(&b, &a));

return 0;
}

void leerConjunto(set<int> *conj, int cantidad) {
for (int i = 0; i < cantidad; ++i) {
int elemento;
cout << "Escribe un elemento: ";
cin >> elemento;
conj->insert(elemento);
}
}

void imprimirConjunto(set<int> *conj) {
set<int>::iterator it;

for (it = conj->begin(); it != conj->end(); it++) {
cout << " " << *it;
}

cout << endl;
}

set<int> opUnion(set<int> *a, set<int> *b) {
set<int> unionSet;
set<int>::iterator it;

for (it = a->begin(); it != a->end(); it++) {
unionSet.insert(*it);
}

for (it = b->begin(); it != b->end(); it++) {
if (unionSet.count(*it) == 0) {
unionSet.insert(*it);
}
}

return unionSet;
}

set<int> opInterseccion(set<int> *a, set<int> *b) {
set<int> interseccion;
set<int> *mayor, *menor;
set<int>::iterator it;

if (a->size() > b->size()) {
mayor = a;
menor = b;
} else {
mayor = b;
menor = a;
}

for (it = menor->begin(); it != menor->end(); it++) {
if (mayor->count(*it) > 0) {
interseccion.insert(*it);
}
}

return interseccion;
}

set<int> opDiferencia(set<int> *a, set<int> *b) {
set<int> diferencia;
set<int>::iterator it;

for (it = a->begin(); it != a->end(); it++) {
if (b->count(*it) == 0) {
diferencia.insert(*it);
}
}

return diferencia;
}

AT THE MOMENT OF COMPILING IT GIVES ME ERROR IN imprimirConjunto(&opUnion(&a, &b)); AND THE OTHERS

    
asked by Guillermo Navarro 24.09.2017 в 23:47
source

1 answer

0

Items returned by a function are temporary elements. If they are not stored in a variable these values are lost at the moment.

What is happening is that the object returned by opUnion is not stored in any variable. Instead, you take your reference and pass it to another function. Since the object is temporary, before executing the function imprimirConjunto the program destroys the temporary object. At the end the pointer that reaches the function is not valid because it points to an object that no longer exists.

To make your program work you have several alternatives. The simplest is to store the returned objects in variables:

set<int> temporal = opUnion(&a,&b);
imprimirConjunto(temporal);

You can also choose to avoid using pointers. In this way a copy of the temporary object will be made and that copy will be the one used within imprimirConjunto :

void imprimirConjunto(set<int>);
    
answered by 25.09.2017 в 07:40