August 11th, 2011, 11:06 AM
#1
[Qt] Pointer auto-deassignment problem.
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];
}
int dictionary::size()
{
return slowa.size();
}
word* dictionary::getWord(int i)
{
if (i<slowa.size() && i>=0)
return &slowa[i];
return 0;
}
----------
class word
{
QString name;
wordclass *parentClass;
QVector<word*> nextWords;
public:
word();
void setParentClass(wordclass *wc);
wordclass* getParentClass();
void addWord(word *w);
QVector<word*> getWords();
void setName(QString n);
QString getName();
};
word::word()
{
}
void word::setParentClass(wordclass *wc)
{
wordclass *w;
w = wc;
this->parentClass = w;
}
wordclass* word::getParentClass()
{
return parentClass;
}
void word::addWord(word *w)
{
nextWords.push_back(w);
}
QVector<word*> word::getWords()
{
return nextWords;
}
void word::setName(QString n)
{
name = n;
}
QString word::getName()
{
return name;
}
----------
class wordclass
{
QVector<word*> words;
int id;
public:
wordclass(int i=0);
void addWord(word*);
void delWord(word*);
int size();
word* getWord(int i);
QVector<word*> getWords();
int getId();
};
wordclass::wordclass(int i):id(i)
{
words.push_back(0);
}
void wordclass::addWord(word *w)
{
this->words.push_back(w);
w->setParentClass(this);
}
void wordclass: elWord(word *w)
{
this->words.remove(words.indexOf(w));
}
int wordclass::size()
{
return words.size();
}
word* wordclass::getWord(int i)
{
return words[i];
}
QVector<word*> wordclass::getWords()
{
return words;
}
int wordclass::getId()
{
return this->id;
}
----------
class wordclasses
{
QVector<wordclass> wektor;
int internalPointer;
public:
wordclasses();
wordclass* addWordClass();
wordclass* getWordClass(int i);
int size();
void merge(int i,int j);
};
wordclasses::wordclasses()
{
internalPointer = 0;
}
wordclass* wordclasses::addWordClass()
{
wordclass nowa(++internalPointer);
this->wektor.push_back(nowa);
return &wektor.last();
}
int wordclasses::size()
{
return wektor.size();
}
wordclass* wordclasses::getWordClass(int i)
{
if (i<wektor.size() && i>=0)
return &wektor[i];
return 0;
}
void wordclasses::merge(int i, int j)
{
wordclass *i_,*j_;
i_ = getWordClass(i);
j_ = getWordClass(j);
QVector<word*> words;
words = i_->getWords();
for (int z=0;z<words.size();z++)
{
words[z]->setParentClass(j_);
i_->delWord(words[z]);
j_->addWord(words[z]);
}
this->wektor.remove(i);
}
----------
----------
And in mainclass:
word *ptr;
ptr = dict.addWord();
ptr->setName("one");
ptr = dict.addWord();
ptr->setName("two");
ptr = dict.addWord();
ptr->setName("three");
doSomethingWithWord();
initWordClasses();
for (int i=0;i<wordClasses.size();i++)
{
wordclass *wc;
wc = wordClasses.getWordClass(i);
qDebug()<<"WC: "<<QString::number(wc->getId());
QVector<word*> wds;
wds = wc->getWords();
for (int j=0;j<wds.size();j++)
{
//CRASHES HERE. There's no ParentClass.
qDebug()<<"->"<<wds[j]->getName()<<wds[j]->getParentClass()->getId();
}
}
I've no idea what's wrong. I guess it's something with pointer assignment, but couldn't figure out what :/
If in method I do something like this:
{
word *ptr;
ptr = variable.getWordPointer();
myVectorOfWordPointers.push_back(ptr);
}
Then it looks like ptr after execution of method disappears, like a normal variable. <br/>And is it allright to do something like this:
ptr* class::method()
{
return &myInnerVectorOfVars[0];
}
Will it return pointer to the element at position 0 in Vector? Or will it return pointer to variable returned from vector [volatile one].
I'm in deep wood, and don't know what to do :/
Code in tar.gz in attachment.
Attached Files
Tags for this Thread
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width