【发布时间】: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:修复抛出异常
【问题讨论】:
-
std:endl有一个冒号,应该是双冒号。 -
错误表明您没有修复丢失的异常规范。顺便看看A Pragmatic Look at Exception Specifications。
标签: c++ inheritance constructor exception