Problem in returning value from class.

Dear all,

I am implementing Depth First Search class of BOOST Graph library, Now i am facing general programming related logical error.

I want to access bolean variable of the class by using get method. but it is not giving the right result.
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

#include "AlgoPC.h"
#include <boost/graph/depth_first_search.hpp>


//=============================Depth First Search===========================
class DFSVisitor: public default_dfs_visitor
{
protected:
	slBayesianNetwork *pBN;
	int nodeA, nodeC;
        bool exist;
	int counter;

public:
	DFSVisitor(slBayesianNetwork *pBN, int nodeA, int nodeC);
	~DFSVisitor();
	void discover_vertex(slNode u, const slGraph &g);
        void resetCounter();
	bool isPathExist();
	int getCounter();
	
};
//--------------------------------------------------------------------------
DFSVisitor::DFSVisitor(slBayesianNetwork *pBN, int nodeA, int nodeC)
{
	this->pBN = pBN;
	this->nodeA = nodeA;
	this->nodeC = nodeC;
        this->exist = false;
	this->resetCounter();
}
//--------------------------------------------------------------------------
DFSVisitor::~DFSVisitor() { }
//--------------------------------------------------------------------------
void DFSVisitor::resetCounter()
{
	this->counter = 0;
	
}
//--------------------------------------------------------------------------

//--------------------------------------------------------------------------
void DFSVisitor::discover_vertex(slNode u, const slGraph &g)
{
	this->counter++; 
	unsigned int varIdx = this->pBN->getVariableIndex(u);
	
		if ((varIdx == this->nodeC ) && ((this->counter) > 2))
		{
			this->exist = true; 
			cout<<"\n "<<counter<<"  node = "<< varIdx+1;
		}
		
	
}
//--------------------------------------------------------------------------
int DFSVisitor::getCounter()
{
	return this->counter;
}
//--------------------------------------------------------------------------
bool DFSVisitor::isPathExist()
{
	return this->exist;
}


Now in my main program
1
2
3
4
5
6

{
							cout<< "\n a = "<<a<<" c = "<<c;
							DFSVisitor vis(this->pBN, a , c);
							depth_first_search((slGraph&)this->pBN->get_graph(),visitor(vis).root_vertex(vertex(a, this->pBN->get_graph())));
							cout<<"\n counter is = "<<vis.getCounter()<<" Path exist = "<< vis.isPathExist();


Error: it gives each time counter value "0" and ispathexist "false"

Hello amanaysin,

Why not ? Probably it blocks for good.
@EverBeginner:

Your name is ever so appropriate. You are truly a master of non-sequiturs.

@amanaysin:

The problem is that the visitor object you pass to depth_first_search is repeatedly copied by the function. You cannot store state in the visitor object self. To solve your problem, you will need to make the data members of the object references to 'external' (ie, outside the class instance) variables.

High,

Not at all.

Just ignore the mindless troll.
Maybe he is one of those guys that got their accounts deleted because they were trying to cheat on tests.
EDIT to my previous post:

Actually, without looking at the declaration and implementation of depth_first_search, my guess is that the visitor object is being copied onto the stack (passed by value) to the function. Anyway, what I said above still applies.

Topic archived. No new replies allowed.