[try Beta version]
Not logged in

 
deque =why elements not changing?

Nov 8, 2012 at 4:06am
//why are actions of ActionB, ActionC not effecting the elements vecP? see end of main.
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include<iostream>
#include<fstream>
#include <cstring>
#include <deque>
#include <vector>
#include <functional>
#include <stdlib.h>
#include <sstream>
#include <math.h>
using namespace std;


struct Ct
{ string Time;
  double  pv;
};

struct vecP
{string method;
string ttacq;
double pv;
int status; 
};

std::deque<Ct>      Dprt; 
std::deque<vecP> Dstore; 


void ActionB(vecP &psn,  Ct &xn) 
{psn.status=2;
psn.method="BLAH-B=Disposed";
psn.pv=xn.pv;
}
void ActionC(vecP &psn,  Ct &xn)
{ psn.status=3;
psn.method="BLAH-C=Disposed";
psn.pv=xn.pv;
}
void ActionA(string meth,  Ct &xn) 
{vecP psn;

psn.status=1;
psn.ttacq=xn.Time;
psn.method=meth;
psn.pv=xn.pv;
Dstore.push_back(psn);
}

void monitor(Ct &xn)
{int i=0,nn=0;
vecP psn;
if (!Dstore.empty()){
	nn=Dstore.size();
     for (std::deque<vecP>::reverse_iterator itr = Dstore.rbegin();  itr != Dstore.rend() ; ++itr, i++){   
	 psn=*itr;
	 if (psn.status==1){
	   ActionB(psn, xn);
		}else{
			if (psn.status==2){
				ActionC(psn, xn);}	
			
		 }//status
		}//for
	 }//if
}
int main()
{ Ct xn;
vecP psn;
int k=0,nn=0;
  

xn.Time="10:00:00";
xn.pv=1000;

Dprt.push_back(xn);
 
ActionA("TestAlgo1",xn);
ActionA("TestAlgo1",xn);

xn.Time="10:30:00";
xn.pv=1500;
Dprt.push_back(xn);

monitor(xn);

if (!Dstore.empty()){
	nn=Dstore.size();
	psn=Dstore.back();
//why are actions of ActionB, ActionC not effectiong the elements vecP?
}
  return 0;
}
Last edited on Nov 8, 2012 at 5:26am
Nov 8, 2012 at 4:08am
Use <> code tags
Nov 8, 2012 at 6:07am
ok - any suggestions?
Nov 8, 2012 at 6:12am
Have you tried stepping through it to see what happens?
Nov 8, 2012 at 6:52am
yes - it changes in the function
but its not reflected when inspected in main -it treats the changes as local
Nov 8, 2012 at 1:43pm
you declare variable vecP psn; locally in your "monitor" function, thus it is only defined within the function's scope.
It will not access nor modify the one you declared in the main function.
Last edited on Nov 8, 2012 at 1:43pm
Nov 8, 2012 at 5:18pm
defining vecP &psn =*itr; fixed the problem, thanks
Topic archived. No new replies allowed.