Re: Good links for handling with pointers?
* Markus Pitha:
Hello,
Alf P. Steinbach schrieb:
Exactly what is the problem?
I often get "segmentation fault" errors because with catchier
constructs, I'm not sure anymore if I should use a reference or a
pointer an so on.
Then often I have the problem that I have a method which should obtain a
non pointer variable, but in this case I only have a pointer to this
variable, and what then?
Let's see my actual example which leads to a "segemntation fault" or
doesn't even compile:
----------------------------------------------------
class Adjazenzmatrix
--------------------
.
.
.
Knoten *knoten[groesse]; //I define a list of size "groesse" of pointers
to "Knoten".
Use a std::vector instead of a raw array.
Use boost::shared_ptr instead of raw pointers.
for (int i = 0; i < groesse; i++) {
knoten[i] = new Knoten(i); initialize every element as new Knoten
.
.
.//and so on.....
//This is weird. Here I want to iterate over every element of "knoten",
but....
for (int kBez = 0; kBez < groesse; kBez++) {
findKomponenten(knoten[kBez]); //LINE 178
}
}
-------------------------------------------
...I get the following error message while compiling:
markus@gentoo ~/CPP-Programme/ $ g++ *.cpp -o Main `pkg-config gtkmm-2.4
--cflags --libs`
Adjazenzmatrix.cpp: In member function ?void
Adjazenzmatrix::berechneEigenschaften()?:
Adjazenzmatrix.cpp:178: Fehler: ung?ltige Umwandlung von ?Knoten*? in ?int?
Adjazenzmatrix.cpp:178: Fehler: Argument 1 von ?Knoten::Knoten(int)?
wird initialisiert
Which means something like:
Invalid conversion of "Knoten*" to "int.
Error: Argument 1 of Knoten::Knoten(int) will be initialized.
What does that mean?
It means you're supplying a pointer to a Knoten instance where a Knoten
instance is required. The compiler tries to convert the pointer to an
instance by passing it to a Knoten constructor that expects an int
argument. The pointer cannot, however, be converted to int.
What you can do is to dereference the pointer:
findKomponenten( *(knoten[kBes]) );
Here is the method findKomponenten:
-------------------------------------------------
void Adjazenzmatrix::findKomponenten(Knoten k) {
k.setVisited();
if (k.getNachbarknotenAnzahl() > 0) {
int nachbarCounter = 0;
Knoten neuer = k.getNachbarknoten(nachbarCounter);
while (neuer.isVisited() == false) {
findKomponenten(neuer);
nachbarCounter++;
}
}
}
-------------------------------------------------
So where is there an int I obviously convert to?
Right below.
Constructor of class Knoten:
------------------------------------------
Knoten::Knoten(int nr) {
knotenNr = nr;
visited = false;
nachbarknoten = new ListT<Knoten>;
}
As you can see, I use a self programmed dynamically list in this class.
Just use std::list instead.
Now it's getting complicated for me. Later in this class, I initialize
new ListT-elements which are also "Knoten". I want to access those
elements from Adjazenzmatrix so I have sometimes already pointer with
the deep of two and then I'm not sure anymore how to handle it.
I don't know if I should return a pointer or not or as I already said,
the return values doesn't "fit" to the class which invokes the methods
of the instances because the method returns a non pointer value but in
the invoking class I have a pointer.
I alwasy fight with such problems, so I need to get deeper into the
knowledge of handling with pointer.
Instead, what you probably need most is to discover that the standard
library's collection classes solve most of the problems you're
struggling with.
Some pointer usage will remain.
For that, use smart pointers such as boost::shared_ptr so that you don't
have to struggle with deallocation and invalid pointers and so on.
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?