C++构造函数报错:request for member ‘TreeisEmpty‘ in ‘T‘, which is of non-class type ‘Tree<char>()‘

代码段:(简化过的)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <stdlib.h>
#include <vector>
using namespace std;
/* 孩子兄弟链表的结点定义 */
template<class ElemType>
struct TreeNode
{
       ElemType data;
       int parent;
       TreeNode<ElemType> *Child, *Sibling;
       TreeNode() : Child(NULL), Sibling(NULL), parent(-1){} //构造函数1,用于构造根结点
};
//树ADT
template<class ElemType>
class Tree{
   private:
      TreeNode<ElemType> *root;   // 头指针
      void TreeDestroy_Cursive( TreeNode<ElemType> *T ); //销毁树(递归准备,private)
   public:
      //无参数的构造函数
      Tree():root(NULL){}
      //带参数的构造函数
      Tree(const ElemType &item){root = new TreeNode<ElemType>(item);}
      //生成树
      void makeTree( const ElemType &item, Tree &child, Tree &sibling);
      //销毁子树
      void ChildDestroy(int flag);
      //返回树结点的个数
      int TreeSize() const{ return TreeSize(root);};
      //判断树是否为空
      bool TreeisEmpty() const{return root == NULL;}
};

int main(){
    Tree<char>T();
     cout<<T.TreeisEmpty();
}

 语句:cout<<T.TreeisEmpty();  报错。

报错原因:

因为无参的构造函数,如Tree<char>T();它可以是个对象,也可是函数声明。但是c++编译器总是优先认为是一个函数声明,然后是对象。

解决方法如下:

1. 不使用()直接初始化对象,比如Tree<char>T;
2. 使用花括号{}初始化对象,比如Tree<char>T{};

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值