C++ 禁止声明没有类型的 'binaryTreeType'

标签 c++ constructor compiler-errors binary-tree enumerated-types

我试过到处移动这段代码,但我无法克服这个编译错误。我需要将 binaryTreeType 传递给 PostorderTreeEnum 的构造函数,因为根指针受到保护,我无法更改它或以任何其他方式访问它。关于如何声明类型的任何网站?我曾尝试将 BinaryTreeType header 和方法放在首位,但这也无法解决问题。任何帮助将不胜感激!!!!

#ifndef H_binaryTree
#define H_binaryTree

#include <iostream>
#include <stack>

using namespace std;

     //Definition of the node
template<class elemType>
struct nodeType
{
   elemType info;
   nodeType<elemType> *llink;
   nodeType<elemType> *rlink;
};

template<class elemType>
class PostorderTreeEnumerator
{
public:
PostorderTreeEnumerator(const binaryTreeType<elemType> *&otherTree);

elemType nextElement();

bool hasMoreElements();

void slideLeft();

void reset();

//friend class binaryTreeType<elemType>;

private:
stack<nodeType<elemType>* > st;


};

template<class elemType>
PostorderTreeEnumerator<elemType>::PostorderTreeEnumerator(const binaryTreeType<elemType> *&otherTree)
{
    slideLeft(this -> root);
}

template<class elemType>
elemType PostorderTreeEnumerator<elemType>::nextElement()
{
    nodeType<elemType> *current, parent;
    st.pop(&current);

    if (!st.isEmpty())
    {
       st.pop(parent);
       if(parent -> rlink != current)
        slideleft(parent -> rlink); 
    }  
}

template<class elemType>
bool PostorderTreeEnumerator<elemType>::hasMoreElements()
{
    return st.isEmpty();
}

template<class elemType>
void PostorderTreeEnumerator<elemType>::slideLeft()
{
  nodeType<elemType> *current;
  current = this -> root;

  while(current != NULL)
  {  
    st.push(current);
    current = current->llink;
  }
  if(st.isEmpty())
    return;
  st.pop(current);
  if(current->rlink != NULL)
    slideleft(current->rlink);
}

template<class elemType>
void PostorderTreeEnumerator<elemType>::reset()
{

}

    //Definition of the class
template <class elemType>
class binaryTreeType
{
public:
    const binaryTreeType<elemType>& operator=
                 (const binaryTreeType<elemType>&);
      //Overload the assignment operator.
    bool isEmpty();
      //Function to determine if the binary tree is empty.
      //Postcondition: Returns true if the binary tree is empty;
      // otherwise, returns false.
    void inorderTraversal();
      //Function to do an inorder traversal of the binary tree.
      //Postcondition: The nodes of the binary tree are output
      // in the inorder sequence.
    void preorderTraversal();
      //Function to do a preorder traversal of the binary tree.
      //Postcondition: The nodes of the binary tree are output
      // in the preorder sequence.
    void postorderTraversal();
      //Function to do a postorder traversal of the binary tree.
      //Postcondition: The nodes of the binary tree are output
      // in the postorder sequence.

    int treeHeight();
      //Function to deteremine the height of the binary tree.
      //Postcondition: The height of the binary tree is returned.
    int treeNodeCount();
      //Function to determine the number of nodes in the
      //binary tree.
      //Postcondition: The number of nodes in the binary tree
      // is returned.
    int treeLeavesCount();
      //Function to determine the number of leaves in the
      //binary tree.
      //Postcondition: The number of leaves in the binary tree
      // is returned.
    void destroyTree();
      //Deallocates the memory space occupied by the binary tree.
      //Postcondition: root = NULL;

void nonRecursiveInTraversal();
void nonRecursivePreTraversal();
void nonRecursivePostTraversal();

    binaryTreeType(const binaryTreeType<elemType>& otherTree);
      //copy constructor

    binaryTreeType();
      //default constructor

    ~binaryTreeType();
      //destructor

void createTree1();

void inorderTraversal(void (*visit) (elemType&));
//Function to do an inorder traversal of the binary tree.
//The parameter visit, which is a function, specifies
//the action to be taken at each node.

PostorderTreeEnumerator<elemType> getPostorderEnumerator();

friend class PostorderTreeEnumerator<elemType>;


protected:
    nodeType<elemType> *root;

private:
    void copyTree(nodeType<elemType>* &copiedTreeRoot,
                  nodeType<elemType>* otherTreeRoot);
      //Function to make a copy of the binary tree to
      //which otherTreeRoot points.
      //Postcondition: The pointer copiedTreeRoot points to
      // the root of the copied binary tree.

    void destroy(nodeType<elemType>* &p);
      //Function to destroy the binary tree to which p points.
      //Postcondition: The nodes of the binary tree to which
      // p points are deallocated.
      // p = NULL.

    void inorder(nodeType<elemType> *p);
      //Function to do an inorder traversal of the binary
      //tree to which p points.
      //Postcondition: The nodes of the binary tree to which p
      // points are output in the inorder sequence.
    void preorder(nodeType<elemType> *p);
      //Function to do a preorder traversal of the binary
      //tree to which p points.
      //Postcondition: The nodes of the binary tree to which p
      // points are output in the preorder sequence.
    void postorder(nodeType<elemType> *p);
      //Function to do a postorder traversal of the binary
      //tree to which p points.
      //Postcondition: The nodes of the binary tree to which p
      // points are output in the postorder sequence.

    int height(nodeType<elemType> *p);
      //Function to determine the height of the binary tree
      //to which p points.
      //Postcondition: The height of the binary tree to which p
      // points is returned.

    int max(int x, int y);
      //Function to determine the larger of x and y.
      //Postcondition: The larger of x and y is returned.

    int nodeCount(nodeType<elemType> *p);
      //Function to determine the number of nodes in the binary
      //tree to which p points.
      //Postcondition: The number of nodes in the binary tree
      // to which p points is returned.

    int leavesCount(nodeType<elemType> *p);
      //Function to determine the number of leaves in the binary
      //tree to which p points.
      //Postcondition: The number of nodes in the binary tree
      // to which p points is returned.

void inorder(nodeType<elemType> *p, void (*visit) (elemType&));
//Function to do an inorder traversal of the binary
//tree, starting at the node specified by the parameter p.
//The parameter visit, which is a function, specifies the
//action to be taken at each node.

PostorderTreeEnumerator<elemType> *postTreeEnum;
};



template<class elemType>
PostorderTreeEnumerator<elemType> binaryTreeType<elemType>::getPostorderEnumerator()
{
return postTreeEnum;
}

这是 sublime 的编译错误。

/Users/jason/dev/CS271/Lab9/binaryTree.h:24: error: expected ',' or '...' before '<' token
/Users/jason/dev/CS271/Lab9/binaryTree.h:24: error: ISO C++ forbids declaration of         'binaryTreeType' with no type
/Users/jason/dev/CS271/Lab9/binaryTree.h:43: error: expected ',' or '...' before '<' token
/Users/jason/dev/CS271/Lab9/binaryTree.h:43: error: ISO C++ forbids declaration of     'binaryTreeType' with no type
[Finished in 1.0s with exit code 1]

最佳答案

因为一个类型必须声明才能使用。现在 binaryTreeTypePostorderTreeEnumerator 中使用后声明。您可以通过为 binaryTreeType 添加前向声明来解决此问题。

template<typename elemType>
class binaryTreeType;

template<class elemType>
class PostorderTreeEnumerator
{
public:
    PostorderTreeEnumerator(const binaryTreeType<elemType> *&otherTree);

    // ... rest of PostorderTreeEnumerator ...
};

关于C++ 禁止声明没有类型的 'binaryTreeType',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16095454/

相关文章:

c++ - 编译器如何为类分配内存?

c++ - 为什么 Xcode 4 在每个 header 中都包含 iostream?

c++ - 派生类的新构造函数未构建 : "overloaded member function not found"

scala - 在Scala中的线对列表上使用模式匹配时发生编译器错误

ios - 错误体系结构armv7的 undefined symbol : “_RELEASE_SAFELY” , referenced from:

c - GCC 格式字符串错误在两个 'format specifies ... but the argument has type ...' 值之间振荡,不喜欢任何一个

c++ - 大负数乘以 -1 给出输出负数 C++

python - 如何将python列表发送到C++应用程序等待输入?

java - 将 ENUM 类型分配给构造函数

c++ - C++ 中有没有一种方法可以在没有构造函数的情况下预初始化结构或类?