Solved Help creating a tree that traverses the map of a graph
-
Hi,
You have a QVector of TreeMap object,
this
is a pointer to the current object. -
Hi
nodes.indexOf(this);
"This" is a const TreeMap *
but it wants a &
int indexOf(const T &t, int from = 0) const;so you can do
int TreeMap::nodeIndex() const {
nodes.indexOf(*this);
}However, QVector wants an assignable data type so you will need some extra functions
http://doc.qt.io/qt-5/containers.html#assignable-data-typesomething like (stripped down)
class TreeMap { public: TreeMap(QString name, TreeMap* parentNode = 0) {} int nodeIndex() const; TreeMap(const TreeMap& other) {/* IMPLEMENT*/} TreeMap& operator=(const TreeMap& other) {/* IMPLEMENT*/} bool operator==(const TreeMap& other) {/* IMPLEMENT*/} // most likely wants this too private: QVector<TreeMap> nodes; };
-
Thanks mrjj.
A few more things to add. You most likely know I'm following the example here:
http://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.htmlWhy is it that the example doesn't implement these functions? QList has the same shortcoming as QVector when I tried changing it. Also, should I start a new thread with the new errors that pop up in my program, or should I continue posting here as they come?
-
That example is using a QList of pointer to TreeItem you are using a QVector of TreeMap object.
-
@Trayon
Hi
The main difference with the sample is that it uses pointers.
QList<TreeItem*>
So it can just compare pointers. ( they are assignable-data-type by nature)You are use a class directly so it need you to tell it how to compare etc.
since it cannot know what members inside that should be used.like if we have
class Car {
QString Model;
}if we have
Car *A = new Car;
and
Car *B = new Car;
we can say if ( A == B ) and it compiler can just check is the memory address is the same.
But if we do
Car A;
Car B;
and say if ( A == B )
then what should it compare.
we can then "explain it" to the compiler with
if ( A.model == B.model )
and that is what we do with
operator==(..)@SGaist (hehe ninjaed)
-
Yes, but as I said, I tried changing it to QList, and it had the same shortcomings. Down the error line, I even saw the "no match for '=='" error for QList as well.
-
@Trayon
Yes, as explained it dont know how to compare your class when its NOT pointers. -
Okay, that explains a lot. Thank you guys for all the help. When I get errors in this project down the line, should I revive this thread, or start a new one?
-
@Trayon
Best with new post with good title so its not mega posts :) -
Just as a last note:
You can also use
std::vector<TreeMap> nodes;
Which can "just work" with your TreeMap since its members
QString nodeName;
TreeMap *parent;Is just copyable but mind the parent pointer as it will just copy it as raw pointer and
it might not be what you want/need.