-1

So I have a pointer SparseMatrix *mat which holds values of the sparsematrix in a triplet format from a file. After I get the values, I would like to be able to just print it out using a print function I created in the SparseMatrix() class. However, I realize that this works if it wasn't a pointer and just 'mat'. I feel like this is something simple but I just cannot wrap my head around it. Any suggestions? Forgive me if this is an extremely noob question. Also, the reason I'm even using a pointer in the first place is because I am trying to visualize a sparsematrix using QT and my professor had given the basic backbone of the code. In that code, a private variable, SparseMatrix *mat was given.

Eric Rangil
  • 133
  • 2
  • 11
  • Through a pointer you access class member functions using the `->` or `*` operator to dereference the pointer. `obj->fun();`/`(*obj).fun();` – πάντα ῥεῖ Mar 29 '15 at 19:36
  • Does `mat->print()` work? – Andreas Vennström Mar 29 '15 at 19:38
  • I have tried those already before. When I try to us mat->print(), the following error is given: window.cpp: In member function ‘void Window::drawMatrix(QPainter&, const SparseMatrix*)’: window.cpp:115:14: error: passing ‘const SparseMatrix’ as ‘this’ argument of ‘void SparseMatrix::print(const string&)’ discards qualifiers [-fpermissive] – Eric Rangil Mar 29 '15 at 19:43
  • 1
    You apparently have a `const` or `const&` reference to the `SparseMatrix` and `print()` is not declared as a `const` function as it probably should be. – πάντα ῥεῖ Mar 29 '15 at 19:47
  • @πάνταῥεῖ You are correct. I don't know how I did not catch that. I changed it to const and it works now. Thanks! – Eric Rangil Mar 29 '15 at 19:59

1 Answers1

0

Via pointers you can access your functions using ->.

Grokking
  • 665
  • 8
  • 16