0

I'm coding my own Tree data structure. In my parent() function, I want to throw no_parent exception that I created. All functions are implemented in Tree.cpp file.

Tree.hpp

#ifndef Tree_hpp
#define Tree_hpp

#include <stdio.h>
#include <vector>
using namespace std;

template<typename T>
class Tree{
    class Node;
    class no_parent : public std::exception {
        virtual const char* what() const _NOEXCEPT
        {
            return "no_parent";
        }
    };

protected:
    Node* rootNode;
    Node* currentNode;
    int _size;
public:
    Tree();
    void addChild(T* childElem);
    void removeChild(int index) throw (out_of_range);
    bool empty();
    int size();
    Tree& root();
    bool isRoot();
    bool isExternal();
    Tree& parent() throw(no_parent);
    Tree& child(int index) throw(out_of_range);
    int getChildrenNum();
    int getChildrenNum(Node* node);
};
#endif /* Tree_hpp */

But when I implement no_parent in Tree.cpp file in this way

template<typename T>
class Tree<T>::no_parent : public std::exception {
    virtual const char* what() const _NOEXCEPT
    {
        return "no_parent";
    }
};

I get error message from parent() function "Incomplete type 'Tree::no_parent' is not allowed in exception specification". How can I implement the no_parent outside of Tree.hpp file??

InSo
  • 21
  • 3

2 Answers2

0

You cannot implement methods depending on template parameters in a source file which is never included where an instance of the class is needed. You either have to include the source file as well or implement your methods in the header right away. See this question for details.

Community
  • 1
  • 1
Ratatwisker
  • 171
  • 1
  • 9
0

I hope your business need you to have nested class and a forward declaration in class Tree. I do not care them much.

But your code works fine with "noexcept" keyword from C++11. http://en.cppreference.com/w/cpp/language/noexcept

I guess the "_NOEXCEPT" does not exist any more. May be in very specific compilers to maintain backward compatibility.

I did few changes to your code and tested, works fine. please find details below.

Removed "stdio" and replaced "iostream", to deal with only C++ (at least the question tag says)

Commented few functions, which are lack of variable declaration.

Here is the code. try it.

#include <iostream>
using namespace std;

template<typename T>
class Tree{
    class Node;
    class no_parent : public std::exception {
        //Added "noexcept" key word here.
        virtual const char* what() const noexcept
        {
            return "no_parent";
        }
    };

protected:
    Node* rootNode;
    Node* currentNode;
    int _size;
public:
    Tree();
    void addChild(T* childElem);
   // void removeChild(int index) throw (out_of_range);
    bool empty();
    int size();
    Tree& root();
    bool isRoot();
    bool isExternal();
    Tree& parent() throw(no_parent);
    //Tree& child(int index) throw(out_of_range);
    int getChildrenNum();
    int getChildrenNum(Node* node);
};

int main()
{
    return 0;
}
Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34