返回指向类定义的结构体的指针,多个作用域操作符
Returning a pointer to a struct defined of a class, multiple scope operators
我的基本二叉树头文件包括以下内容:
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: error: expected constructor, destructor, or type conversion before ‘*’ token
我正在编译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
。
相关文章:
- 未在作用域中声明unordered_map
- 有没有一种方法可以在编译时获得作用域类名
- C++quit()函数中可能存在作用域问题
- 未在此作用域OpenCV3.4中声明cvSaveImage
- 全局作用域中函数指针的赋值
- 在类函数中初始化外部作用域变量
- 不同作用域中的静态变量和全局变量
- 重载操作符+:表达式必须是整型或无作用域枚举类型
- 返回指向类定义的结构体的指针,多个作用域操作符
- 奇怪语法:作用域操作符(::)后面有星号
- 作用域操作符的不同用法
- 为什么c++的作用域解析操作符是::
- 在Visual Studio 2012中,作用域操作符中的错别字会导致意外的编译和递归
- ::操作符能否出现在与限定名查找中的作用域解析不同的上下文中?
- 作用域,操作符,函数错误需要帮助
- c++的new操作符作用域
- 在类定义的类名中,作用域解析操作符的作用是什么?
- 关于c++中作用域解析操作符的问题
- 为什么基类构造函数没有被作用域解析操作符调用
- 命名空间作用域中的操作符在全局作用域中隐藏另一个操作符