简体   繁体   English

再次将指针传递给函数时遇到麻烦

[英]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) 试图将我的二进制搜索树(BST)的根传递给UI函数(我需要将其作为可修改的变量传递,或者将其称为)

main.cpp 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: 标头中的UI类具有:

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

and in the .cpp file: 并在.cpp文件中:

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> * 我得到的错误:函数认为BST<Matrix>&*时,它认为BST<Matrix>&* BST<Matrix> *

I don't plan on using C++ much for now, so a detailed answer (while appreciated) is not necessary. 我暂时不打算使用C ++,所以不需要详细的答案(虽然很感激)。

Your function signature should look like 您的功能签名应如下所示

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

instead of 代替

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

Firstly BST<Matrix *> is not the same as BST<Matrix>* . 首先, BST<Matrix *>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);

You have created 您已创建

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

but the function asked for a BST<Matrix*> argument. 但是该函数要求一个BST<Matrix*>参数。 Note the subtle difference 注意细微的差别

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM