简体   繁体   English

使用继承了std :: exception的嵌套类抛出异常

[英]throw a exception with nested class that inherited std::exception

I'm coding my own Tree data structure. 我正在编码自己的Tree数据结构。 In my parent() function, I want to throw no_parent exception that I created. 在我的parent()函数中,我想抛出我创建的no_parent异常。 All functions are implemented in Tree.cpp file. 所有功能都在Tree.cpp文件中实现。

Tree.hpp 树.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 但是当我以这种方式在Tree.cpp文件中实现no_parent时

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". 我从parent()函数收到错误消息“异常规范中不允许使用不完整的类型'Tree :: no_parent'”。 How can I implement the no_parent outside of Tree.hpp file?? 如何在Tree.hpp文件之外实现no_parent?

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. 有关详细信息,请参见此问题

I hope your business need you to have nested class and a forward declaration in class Tree. 我希望您的业务需要您在类Tree中具有嵌套类和前向声明。 I do not care them much. 我不太在乎他们。

But your code works fine with "noexcept" keyword from C++11. 但是您的代码可以与C ++ 11中的“ noexcept”关键字一起正常工作。 http://en.cppreference.com/w/cpp/language/noexcept http://en.cppreference.com/w/cpp/language/noexcept

I guess the "_NOEXCEPT" does not exist any more. 我猜“ _NOEXCEPT”已经不存在了。 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) 删除了“ stdio”并替换了“ iostream”,以仅处理C ++(至少是问号说)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM