二叉搜索树例程无法使用指针参数进行编译
Binary search tree routines fail to compile with pointer arguments
C++来说很新,我正在尝试为几天后到期的项目编写二进制堆计算器。在我进入二进制堆之前,我想编写一个二叉树结构作为堆的超类。
我仍在尝试围绕指针与引用以及每个在分配时的外观以及何时应该将某些内容定义为指针或引用。
无论如何,这里有一些我好奇的代码:
#include "BinaryTree.h"
int main(void){
BinaryTree tempTree = new BinaryTree();
BinaryNode* ptrToRoot;
ptrToRoot = tempTree.getRootNode();
int inputArr = { 5, 2, 7, 10, 11, 20, 1};
for(int i = 0; i < sizeof(inputArr) / sizeof(inputArr[0]); i++){
tempTree.binaryTreeInsert(ptrToRoot, inputArr[i]);
}
tempTree.inOrderPrint(ptrToRoot);
}
而且我从对binaryTreeInsert和inOrderPrint的调用中都得到了错误,两者都将ptrToRoot作为参数。错误显示"无效参数...有效的候选者是 BinaryNode *, int。
但是当我将鼠标悬停在 Eclipse 中的每个参数上时,它们都显示它们是必要的类型。
我是否错误地定义了指针?这是我的BinaryTree类的头文件,以防它有帮助:
#ifndef BINARYTREE_H_
#define BINARYTREE_H_
#include "BinaryNode.h"
struct BinaryTree {
BinaryTree();
virtual ~BinaryTree(){}
BinaryNode rootNode;
int noOfNodes;
BinaryNode* getRootNode(){ return rootNode; }
int countNodes(BinaryNode* ptRootNode);
bool binaryTreeContains( BinaryNode* ptRootNode, int element);
void binaryTreeInsert(BinaryNode* ptRootNode, int element);
void preorderPrint( BinaryNode *ptRootNode );
void postorderPrint( BinaryNode *ptRootNode );
void inorderPrint( BinaryNode *ptRootNode );
};
#endif
这可能至少是您问题的一部分:
BinaryTree tempTree = new BinaryTree();
此行不正确; new
用于执行堆分配,并返回指向新分配对象的指针。 但是,您的对象是在堆栈上分配的。
尝试将其更改为:
BinaryTree tempTree;
这将使用 no-arg 构造函数在堆栈上构造一个新对象。 这可能会解决您的问题,因为编译器可能会对此变量的类型感到困惑。
相关文章:
- 使用指向成员的指针将成员函数作为参数传递
- 如何使用Visual Studio 2017在C++中为参数化对象数组使用唯一指针
- 修改函数中的指针(将另一个指针作为参数传递)
- Clang bug?使用指针作为模板参数
- MSVC将仅移动结构参数解释为指针
- 非类型指针和引用模板参数,以及在编译时如何/为什么解析它们.c++
- 如何使用非类型模板参数传入指向数组的指针
- 将成员函数指针作为参数传递给模板方法
- 匹配函数指针作为模板参数?
- 当指向对象的指针作为参数传递给 std::thread 时,内存可见性
- 通过智能指针和转换对基本模板参数进行模板推导
- 为函数定义符号不明确的指针参数
- 使用引用与指针将数组作为参数传递
- 如何将成员函数作为回调参数传递给需要"typedef-ed"自由函数指针的函数?
- C++ - 声明指向返回任何类型并获取任意数量参数的函数的指针
- 将函数参数完美转发到函数指针:按值传递呢?
- 保留函数指针模板参数
- 如何构造一个以可变参数指针函数作为成员的类?
- 指向对象的非类型模板参数指针
- c++ 使用 *& as 参数(指针乐趣,三级树方法)