views:

62

answers:

2

My question is rather simple, but I am stuck. How can I choose the desired constructor from base class?

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

I get:

$ g++ InternalNode.cpp

InternalNode.cpp:16: error: declaration of ‘InternalNode::InternalNode(double, double, std::vector >&, int, int) throw (WrongBoundsException)’ throws different exceptions InternalNode.h:17: error: from previous declaration ‘InternalNode::InternalNode(double, double, std::vector >&, int, int)’

UPDATE 0: Fixed missing :

UPDATE 1: Fixed throw exception

+1  A: 

You are missing the exception specification for the constructor definition:

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;
}
Georg Fritzsche
+2  A: 

This simplified code compiles correctly, but doesn't link becuase of missing constructor definition for base class:

#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)
{
}

BTW, why do you feel the need to use exception specifications? They are generally seem as a bit of a waste of time in C++.

anon
Thanks for the suggestion. I don't use exceptions anymore.
myle
@myle: Exceptions are good. Its "Exception Specifications" that Neil is referring too.
Martin York
@myle Yes - you need exceptions, particularly for reporting construction errors, just don't use specifications.
anon
Thanks Martin for claryfing it. I removed the Exception Specifications but keep on using exceptions. Thanks Neil Butterworth too!
myle