gpt4 book ai didi

c++ - 返回指向由类、多个范围运算符定义的结构的指针

转载 作者:搜寻专家 更新时间:2023-10-31 01:55:30 24 4
gpt4 key购买 nike

我的基本二叉树头文件包括以下内容:

template<typename T>
class Btree{
// OVERVIEW: a binary tree with flexible structure that is not sorted

private:

struct node { //a container object
node *left; //left and right tree
node *right;
T *o; // pointer to object of node
};

public:

node *root; //pointer to the root of the tree (NULL if empty)

node* insert (node *parent, T *child, int child);
//MODIFIES: this
//EFFECTS: creates a node that stores a pointer to the new child
// and returns the pointer to the node of the new child
// the integer child is either 0, for left child,
// or anything else for right child

// void printTree (node * root);
//EFFECTS: takes the root of a tree and prints the tree's
// coordinates

Btree(){}; //ctor
Btree(){} //dtor


};

#include "btree.cpp"

我的 .cpp 看起来像这样,请注意它包含在我的标题底部以避免模板编译器错误:

   template <typename T>
Btree<T> :: node * Btree<T>::insert (node *parent, T *child, int child)

{
node *np = new node;
np-> o = child;
np->left = NULL;
np->right = NULL;
if (child == 0)
parent->left = np;
else
parent->right = np;
return np;
}

但是,我得到以下编译器错误:

btree.cpp:3: 错误:在“*”标记之前需要构造函数、析构函数或类型转换

我正在使用 g++ 版本 4.1.2 进行编译。

有人能帮忙吗?

最佳答案

首先,你的析构函数前面应该有一个~,所以改

Btree();    //ctor
Btree(){} //dtor

Btree();    //ctor
~Btree(){} //dtor

其次,insert的返回类型前需要typename,因为它是依赖类型:

   template <typename T>
typename Btree<T>::node* Btree<T>::insert(node *parent, T *child, int child)
// ^^^^^^^^ <- needed
{
node *np = new node;
np-> o = child;
np->left = NULL;
np->right = NULL;
if (child == 0)
parent->left = np;
else
parent->right = np;
return np;
}

您还需要重命名您的参数之一,您有两个已命名的 child

关于c++ - 返回指向由类、多个范围运算符定义的结构的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8379667/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com