Kalec
Kalec

Reputation: 2881

Having trouble passing a pointer to a function, again

Trying to pass the root of my Binary Search Tree (BST) to the UI function (I need to pass it as a modifiable variable or however it's called)

main.cpp

cmd = UI.uiCmd()

BST<Matrix> *data = new BST<Matrix>;
Matrix mat;

UI.handle (cmd, mat, data); // passing command, class object, root of BST

The UI class in the header has:

private:
void handle (int, Matrix, BST<Matrix *>);

and in the .cpp file:

void ui::handle(int cmd, Matrix matrix, BST<Matrix *> data)

I know I'm messing up somewhere but I can't say where, I have a very poor grasp of pointers

The error I get: it thinks BST<Matrix>&* while function asks BST<Matrix> *

I don't plan on using C++ much for now, so a detailed answer (while appreciated) is not necessary.

Upvotes: 0

Views: 118

Answers (3)

Rook
Rook

Reputation: 6145

Firstly BST<Matrix *> is not the same as BST<Matrix>*. One is a container of pointers, the other is a pointer to a container.

Secondly, if you want to let the function modify an argument, you can pass it by reference:

void ui::handle(int cmd, sMat matrix, BST<sMat>& data) 

and call it like

cmd = UI.uiCmd() 

BST<Matrix> data;
Matrix mat; 

UI.handle(cmd, mat, data);

Upvotes: 2

mathematician1975
mathematician1975

Reputation: 21351

You have created

    BST<Matrix> *data = new BST<Matrix>;

but the function asked for a BST<Matrix*> argument. Note the subtle difference

    BST<Matrix> * IS NOT same as  BST<Matrix*>

Upvotes: 1

TaZ
TaZ

Reputation: 743

Your function signature should look like

void handle (int, Matrix, BST<Matrix>*)

instead of

void handle (int, Matrix, BST<Matrix *>)

Upvotes: 3

Related Questions