473,385 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Reference, but object unchanged?

Hello,

in an recursion I try to change the elements called "Knoten" in a vector
vector<KnotenvecKnoten.
It's some kind of depth-first search. What I want to do is to find the
nodes of a graph which are connected. I think my algorithm works, but
changing the values to "visited = true" doesn't work due to any
reference problem.

First of all, I fill up the vector with every node (getWert() does this
job, j is a value of the column of the adjacency matrix which describes
the graph, so every value != 0 is a neighbour node of *k)

================================================== =
CLASS Adjazenzmatrix:
..
..

vector<KnotenvecKnoten;
..
..

for (int i = 0; i < groesse; i++) {
Knoten *k = new Knoten(i);
for (int j = 0; j < groesse; j++) {
if (getWert(i,j) != 0)
k->addNachbarknoten(j);
}
vecKnoten.push_back(*k);

}

---------------------------------

Now, this loop searches for components. So if every node is connected to
each other in my graph, findKomponenten() must only be invoke at once by
this loop, but it doesn't and this is not all as you will see then.

for (int kBez = 0; kBez < groesse; kBez++) {
if (vecKnoten.at(kBez).isVisited() == false) {
cout << "new Component" <<endl;
findKomponenten(&vecKnoten.at(kBez));
}
}

This is this function and the algorithm. Here you can see very good,
that the changing of the nodes doesn't even work in the same loop?
So no wonder why it doen't work outside the loop either. But what's the
problem?

void Adjazenzmatrix::findKomponenten(Knoten *k) {

if ((*k).isVisited() == false) {
(*k).setVisited();
cout << "Knoten " << (*k).getKnotennummer()+1 << ": (auf visited
gesetzt)" <<endl;
int nachbarCounter = 0;

while (nachbarCounter < (*k).getNachbarknotenAnzahl()) {

if ((*k).getNachbarknoten(nachbarCounter).isVisited() == false) {

cout << "Not visited yet: " <<
(*k).getNachbarknoten(nachbarCounter).getKnotennum mer()+1 <<endl;
(*k).getNachbarknoten(nachbarCounter).setVisited() ;
//ATTENTION: This following if is not needed for this program, but I
added it for testing and to see if setVisited() work, but it doesn't!
if
if ((*k).getNachbarknoten(nachbarCounter).isVisited() == false)
cout << "STILL NOT VISITED: " <<
(*k).getNachbarknoten(nachbarCounter).getKnotennum mer()+1 <<endl;

findKomponenten(&(*k).getNachbarknoten(nachbarCoun ter));
}
nachbarCounter++;
}
}
}

By the way, this line
findKomponenten(&(*k).getNachbarknoten(nachbarCoun ter)); leads to a
weird warning:
"Adjazenzmatrix.cpp:264: Warnung: Adresse eines temporären Wertes wird
ermittelt"
It means something like: address of a teporary value determined.
I guess it is ok due to the recursion, isn't it?
==================================================

For the better understanding, here are the invoked methods.
==================================================
CLASS Knoten

#include "Knoten.h"

Knoten::Knoten() {
knotenNr = 0;
visited = false;
}

Knoten::Knoten(int nr) {
knotenNr = nr;
visited = false;
}

Knoten::~Knoten() {
}

void Knoten::setKnotennummer(int n) {
knotenNr = n;
}

int Knoten::getKnotennummer() {
return knotenNr;
}

void Knoten::addNachbarknoten(Knoten k) {
nachbarknoten.push_back(k);
}

int Knoten::getNachbarknotenAnzahl() {
return nachbarknoten.size();
}

Knoten Knoten::getNachbarknoten(int knr) {
return nachbarknoten.at(knr);
}

void Knoten::setVisited() {
visited = true;
}

bool Knoten::isVisited() {
return visited;
}
=================================================
I have no clue what's wrong with the references, but I hope you will see
my mistakes.

Thanks,
Markus
Oct 26 '07 #1
2 1161
On Oct 26, 11:35 am, Markus Pitha <newsgroupsNOS...@pithax.netwrote:
[snip]
vector<KnotenvecKnoten;
.
.

for (int i = 0; i < groesse; i++) {
Knoten *k = new Knoten(i);
for (int j = 0; j < groesse; j++) {
if (getWert(i,j) != 0)
k->addNachbarknoten(j);
}
vecKnoten.push_back(*k);

}
This is a memory leak, isn't it? A copy of the
object gets put in the vector. Each time you go
through this loop you will lose some memory that
never gets deleted.

I dunno what your problem is with not having the
content of the vector changed. Maybe you can reduce
the program to a minimal compilable code that shows
the problem.
Socks

Oct 26 '07 #2
Hello,

Puppet_Sock wrote:
This is a memory leak, isn't it? A copy of the
object gets put in the vector. Each time you go
through this loop you will lose some memory that
never gets deleted.
You are right. I corrected this mistake in my main program.
I dunno what your problem is with not having the
content of the vector changed. Maybe you can reduce
the program to a minimal compilable code that shows
the problem.
Socks
Ok, I've done it.
YOu can download the files here: http://test.pithax.net/referencetest.zip

It simulates a graph with actually 2 component.
Component 1 is a line where node 1 is connected to node 4.
Component 2 is a triangle of node 2, 3 and 5.

You will see that setVisited() won't work, so it passes every "visited"
node too.
Markus
Oct 26 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ryan Hubbard | last post by:
I would like to use a static variable inside a method in an object to hold a reference to another object. But i can't get it to work. Here is an example class B{ var $test; } class A{...
1
by: Christopher | last post by:
Hi all, I am trying to store a reference to a variable in my data providerr class but cannot save a reference to it. Can anyone help me? I would like this code to allow me to store any field...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
7
by: Xiaoshen Li | last post by:
Dear All, I thought I understood using pointer variables as function parameters. But I failed to understand why it is needed pass-by-reference of a pointer variable. To me, pointer variable...
11
by: Richard Lewis Haggard | last post by:
Is it possible to put a reference to an object inside of a class? If so, what is the syntax? The reason I want to do this is so that I can make a copy of the original object, make modifications to...
2
by: Anders Borum | last post by:
Hello! I was looking at marking objects with a changed state, once properties have been changed. The reason behind this is, that I would like to enlist such objects for processing in a...
13
by: al.cpwn | last post by:
I get that these two are different int* get() { static int m; return &m; } int& get() {
5
by: Michael Russell | last post by:
Hi all, Using C#, I've created a simple wrapper class for using Excel. I have Office Pro 2003 installed on my devel machine. The wrapper class works great, reading and writing to/from Excel. ...
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.