0

저는이 코드를 계속 옮겨 보았습니다.이 컴파일 오류를 지나칠 수 없습니다. PostTreeEnum의 생성자에 binaryTreeType을 전달해야합니다. 왜냐하면 루트 포인터가 보호되어 있고 다른 방법으로 변경할 수 없기 때문입니다. 유형을 선언하는 방법에 대한 설명은 없습니까? BinaryTreeType 헤더 및 메서드를 먼저 넣으려고했으나 해결되지 않았습니다. 어떤 도움을 많이 주시면 감사하겠습니다 !!!!C++은 타입이없는 'binaryTreeType'의 선언을 금지합니다.

#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; 
} 

여기는 숭고한 것에서 컴파일 오류입니다.

/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] 

답변

1

형식을 사용해야하기 때문에 선언해야합니다. 현재 binaryTreeTypePostorderTreeEnumerator에서 사용 된 후에 선언됩니다. binaryTreeType에 대한 전달 선언을 추가하여이 문제를 해결할 수 있습니다.

template<typename elemType> 
class binaryTreeType; 

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

    // ... rest of PostorderTreeEnumerator ... 
}; 
+0

정말 고마워요! 정말 감사! – x2z