exchangeing the values of two instantces of a class

Hi guys.

I would like to exchange the values of two instances of an object. To make my self clear to others here is an example for int number

...

int a=1; int b=2; int c;

c=a;
a=b;
b=c;

...

and here the real code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "Keller.hpp"
#include "datastructure.h"
#include <stdlib.h>
#include <iostream>
using namespace std;

int exchange(Keller& D,Keller& E){
  Keller C;
  C=D;
  //cout<<"print C"<<endl;
  //C.print();
  D=E;
  E=C;
  //cout<<"print B"<<endl;
  //E.print();
  //cout<<"print A"<<endl;
  //D.print();
}

int main(){
  Keller A,B;
  datastrct data;
  int check;
  cout<<"Enter data for A"<<endl;
  for (int dum=0;dum<=dim-1;dum++){
    cin>>data.list[dum];
  }
  A.push(data);
  cout<<"next value"<<endl;
  for (int dum=0;dum<=dim-1;dum++){
    cin>>data.list[dum];
  }
  A.push(data);
  
  cout<<"Enter data for B"<<endl;
  for (int dum=0;dum<=dim-1;dum++){
    cin>>data.list[dum];
  }
   B.push(data);
  check=exchange(A,B);
  cout<<"print A"<<endl;
  cout<<endl;
  A.print();
  cout<<"print B"<<endl;
  cout<<endl;
  B.print();
}



So far so good. What happens is that the first number of my datastructure datastrct is either zero or some arbitrary number. Probably those are addresses.
As you can see I tried to debug by printing out the exhanged values within the function exchanged. That worked fine. The whole program works also if I just use datastrct and not the class Keller. The class Keller is just a textbook lifo datastructure. datastructure.h is where I define the element of the Keller
lifo datastructure. Question is of course why it doesn't work. If there is a smarter why to do it, by using pointers or so that would be just as helpful.

Thank you in advance.

sbk.




Topic archived. No new replies allowed.