【问题标题】:Inheritance and choose constructor from base class从基类继承和选择构造函数
【发布时间】:2010-06-18 10:48:51
【问题描述】:

我的问题很简单,但我被卡住了。如何从基类中选择所需的构造函数?

// node.h
#ifndef NODE_H
#define NODE_H

#include <vector>

// definition of an exception-class
class WrongBoundsException
{
};

class Node
{
    public:
        ...

        Node(double, double, std::vector<double>&) throw (WrongBoundsException);
        ...
};

#endif


// InternalNode.h
#ifndef INTERNALNODE_H
#define INTERNALNODE_H

#include <vector>
#include "Node.h"


class InternalNode : public Node
{
    public:
        // the position of the leftmost child (child left)
        int left_child;
        // the position of the parent
        int parent;

        InternalNode(double, double, std::vector<double>&, int parent, int left_child) throw (WrongBoundsException);

    private:
        int abcd;

};

#endif


// InternalNode.cpp

#include "InternalNode.h"

#define UNDEFINED_CHILD -1
#define ROOT -1


// Here is the problem
InternalNode::InternalNode(double a, double b, std::vector<double> &v, int par, int lc) 
throw (WrongBoundsException)
: Node(a, b, v), parent(par), left_child(lc)
{
    std::cout << par << std::endl;
}

我明白了:

$ g++ InternalNode.cpp

InternalNode.cpp:16: 错误:'InternalNode::InternalNode(double, double, std::vector >&, int, int) throw (WrongBoundsException)' 的声明抛出不同的异常 InternalNode.h:17:错误:来自先前的声明'InternalNode::InternalNode(double, double, std::vector >&, int, int)'

更新 0:修复缺失:

更新 1:修复抛出异常

【问题讨论】:

标签: c++ inheritance constructor exception


【解决方案1】:

此简化代码编译正确,但由于缺少基类的构造函数定义而无法链接:

#include <vector>

// definition of an exception-class
class WrongBoundsException {
};

class Node {
    public:
        Node(double, double, std::vector<double>&) 
                throw (WrongBoundsException);
};

class InternalNode : public Node {
    public:
        // the position of the leftmost child (child left)
        int left_child;
        // the position of the parent
        int parent;

        InternalNode(double, double, std::vector<double>&, 
                        int parent, int left_child) 
                        throw (WrongBoundsException);
    private:
        int abcd;

};

// Note added exception specification
InternalNode::InternalNode(double a, double b, 
                            std::vector<double> &v, 
                    int par, int lc) throw (WrongBoundsException)
        : Node(a, b, v), parent(par), left_child(lc)
{
}

顺便说一句,您为什么觉得需要使用异常规范?在 C++ 中,它们通常看起来有点浪费时间。

【讨论】:

  • 感谢您的建议。我不再使用异常。
  • @myle:例外是好的。 Neil 也提到了它的“异常规范”。
  • @myle 是的 - 你需要例外,特别是报告构造错误,只是不要使用规范。
  • 感谢 Martin 的澄清。我删除了异常规范,但继续使用异常。也感谢 Neil Butterworth!
【解决方案2】:

您缺少构造函数定义的异常规范:

InternalNode::InternalNode(double a, double b, std::vector<double> &v, int par, int lc) 
  throw (WrongBoundsException)
  : Node(a, b, v), parent(par), left_child(lc)
{
    std::cout << par << std::endl;
}

【讨论】:

    猜你喜欢
    • 2017-11-17
    • 2013-10-24
    • 1970-01-01
    • 2012-09-03
    • 2011-05-31
    • 2015-11-03
    • 1970-01-01
    • 2015-12-02
    相关资源
    最近更新 更多