Solved Class
-
Hello,
why I can not add any more variables to my class? If I add a new variable to the class, my program crashes.#ifndef GRAPH_H #define GRAPH_H #include <QtCore> #include <QtGui> class Graph { public: Graph(); int width = 40; int height = 30; int Vertices; void initialize_matrix(int nodes){ Vertices = nodes; matrix->clear(); store_searching_path.clear(); store_path.clear(); matrix = new QVector <int> [nodes]; } void addEdge(int vertice1, int vertice2){ matrix[vertice1].push_back(vertice2); } QVector <int> store_searching_path; QVector <int> store_path; bool BFS(int start, int end); bool DFS(int start, int end); //void recursive_DFS(int source); void dijkstra_algorithm(); private: QVector <int> *matrix; //bool *visited; int blabla; }; #endif // GRAPH_H
For example when I add blabla integer my program crashes, if I delete blabla integer then everything is fine
-
Your
blabla
int member itself is most likely not the reason for your crash. Where do you use it? -
@Pl45m4
That's the point, I don't use it, I added blabla for the example -
Where exactly crashes your program? Any error message?
@privatepepper said in Class:
int width = 40; int height = 30; int Vertices;
What's the reason for them being public?
-
I get only one error - The program has unexpectedly finished.
I use width and height in the mainwindow class to draw grid of QGraphicsRectItems , and I just realized that Vertices should be private, because I use Vertices only in this class, but now when i add Vertices to private and delete blabla integer my program too crashes;/
-
Hi,
Then use the debugger to see what is happening.
-
@privatepepper said in Class:
I use width and height in the mainwindow class to draw grid of QGraphicsRectItems
There are fancy things called setters / getters :-)
-
@Pl45m4
what do you mean by that?I found a way to fix my code. But I don't know why this thing works and what is that. When I started to use the debugger on my ide opened like another file, it looked like behind the scenes what qt is doing.
Little part of that code:inline void QVector<T>::clear() { //if (!d->size) // return; // destruct(begin(), end()); // d->size = 0; }
And if i comment out these lines, my code works fine and i can add new variables to my class:). Could someone explain what is this and why my code didn't work before?
-
As I already suggested, use the debugger.
-
@SGaist
Yeah I know, and thanks for the help:), I already fixed my code as I mentioned above, but I don't understand what I fixed:/ -
If you are talking about the QVector snippet, you did not fix anything, you broke QVector.
-
@privatepepper
Your comment-out code preventsQVector<T>::clear()
from actually clearing its elements, and then your code works. I would look at everywhere you use aQVector
. You havematrix->clear();
; just what do you do with yourQVector <int> *matrix
? It is a member variable right next to where you introduce a new variable and then it crashes....matrix->clear(); ... matrix = new QVector <int> [nodes];
What about first time round? You do initialize
matrix
before this, don't you, else you're trying toclear()
a random area of memory.... -
-
There is also no need to use a pointer here.
-
@Christian-Ehrlicher said in Class:
There is also no need to use a pointer here.
Do you mean
QVector <int> *matrix
? I added this pointer because I want that this vector would be 2d -
Then use
QVector<QVector<int>> matrix
-
Okay, I will use
QVector<QVector<int>> matrix
, but why I shouldn't use a pointer? Is there a big difference? Sorry for dumb questions... -
@privatepepper said in Class:
but why I shouldn't use a pointer?
Because it's not needed and when using an object you can't forget to initialize (and delete) it (as you did both)