Hello there.
I've been struggling with this problem for 2 days [~30hours]. I'm writing my master thesis, and I'm stuck with pointers. Below I'll present some sample code. But what's the problem?
I have lots of QVector<myClass*> and when I'm operating on these myClass pointers values sometimes disappear. In sample below it, when I merge two WordClasses, parentClass of one of the words disappears. Don't know why.
Ok. Here are the classes:
----------
class dictionary
{
QVector<word> slowa;
public:
dictionary();
word* addWord();
int size();
word* getWord(int i);
};
---------
word* dictionary::addWord()
{
word nowe;
this->slowa.push_back(nowe);
return &slowa[slowa.size()-1];
}
Hello there.
I've been struggling with this problem for 2 days [~30hours]. I'm writing my master thesis, and I'm stuck with pointers. Below I'll present some sample code. But what's the problem?
First, please re-edit your code to use code tags. The code you posted is practically impossible to read if it is not formatted using code tags.
I have lots of QVector<myClass*> and when I'm operating on these myClass pointers values sometimes disappear. In sample below it, when I merge two WordClasses, parentClass of one of the words disappears. Don't know why.
Have you used the debugger that comes with your compiler suite? The debugger is the tool that you are supposed to use to debug programs and figure these things out.
Code:
return &myInnerVectorOfVars[0];
}
Will it return pointer to the element at position 0 in Vector?
I have no idea what a QVector is, but if it were std::vector, then that line of code returns the address of the first element in the vector, given that the vector is not empty.
2) In many places you return the address of an element in the QVector ...
Code:
return &slowa[i];
Do you store those values and use them later ? If so, there would be a problem
if you add to the slowa vector in the above example. The address might not
be valid do to internal re-allocation in QVector
All of you, I'm sorry for not using code syntax, in hurry couldn't find it.
Philip, and that's what I was worrying about [pt 2].
How can I avoid this?
I have 2 classes which have inside QVector<someClass>.
And lots of classes that use pointers to that someClass. Maybe instead of using QVector I should use ordinary array?
[sorry for writing another post, but couldn't find "edit this post" button]
and what If I have QVector<someClass*> vector and I want to return one of the pointers?
Is
return vector[0];
returning someClass* pointer? or something else? [I know, stupid question, but I got confused]
All of you, I'm sorry for not using code syntax, in hurry couldn't find it.
Then your code will remain unread by many that could help you:
Code tags are this: [code] Your code goes here [/code]
Second, please post this "word" class, as it is important and plays a big role in your code now, since you are storing pointers to it.
Why do you need to make a Qvector of word pointers? What's wrong with just QVector<word>? Then you would eliminate a lot of these pointer manipulations you have going on now, if "word" need not be stored as pointers.
1) I'f you'd be so kind to tell me where can I edit my post.
2) why do I need QVector of word pointers? Because I need to have only one instance of word appearing in many classes like
- word needs to know what was next/previous word;
- wordClass needs to know what words are in it;
- sentence needs to know what words it have;
and when I change for example word's string, this change must be seen in other classes.
when reallocating does whole vector move? or only it's elements?
Following this http://doc.qt.nokia.com/qq/qq19-containers.html, if I understand properly, the location of vector's head is persistent, only the elements move? So I could for example store vector's position and in place of someClass* store integer working as an offset? So calling someClass* would be equivalent to vector_s_position+int?
when reallocating does whole vector move? or only it's elements?
Following this http://doc.qt.nokia.com/qq/qq19-containers.html, if I understand properly, the location of vector's head is persistent, only the elements move? So I could for example store vector's position and in place of someClass* store integer working as an offset? So calling someClass* would be equivalent to vector_s_position+int?
Assuming QVector works similar to std::vector, you cannot safely store a pointer to any element in the vector when you add elements to it. That holds for the first element just as well. However, std::vector has an overloaded operator [] that allows you to retrieve any element using an index. So you could store indices instead of pointers and access the element using operator [], as long as you don't remove elements from the vector, of course.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.