Advertisement
Guest User

Overloaded constructor ambiguous

a guest
Nov 27th, 2014
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.21 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class Node {
  4.     private:
  5.         Node *parent, *left, *right;
  6.         int value;
  7.     public:
  8.         // Constructors
  9.         Node(const Node&);
  10.         Node(const int&, Node* = nullptr, Node* = nullptr, Node* = nullptr);
  11.         Node(const int&, Node*, Node*, const int&);
  12.         Node(const int&, Node*, const int&, Node*);
  13.         Node(const int&, Node*, const int&, const int&);
  14.         Node(const int&, Node*, const int&);
  15.         Node(const int&, const int&, Node* = nullptr);
  16.         Node(const int&, const int&, const int&);
  17.         Node(const int&, Node* = nullptr);
  18.         Node(const int&);
  19.         ~Node();
  20.         // Set-methods
  21.         void setparent(Node*);
  22.         void setvalue(const int&);
  23.         // Get-methods
  24.         Node& getParent() const;
  25.         Node& getLeft() const;
  26.         Node& getRight() const;
  27.         const int& getValue() const;
  28. };
  29.  
  30. Node::Node(const Node& node) {
  31.     this->value = node.getValue();
  32.     this->parent = &node.getParent();
  33.     this->left = &node.getLeft();
  34.     this->right = &node.getRight();
  35. }
  36.  
  37. Node::Node(const int& value, Node* parent, Node* left, Node* right) {
  38.     this->value = value;
  39.     if (parent != nullptr) {
  40.         this->parent = parent;
  41.     }
  42.     if (left != nullptr) {
  43.         this->left = left;
  44.     this->left->setparent(this);
  45.     }
  46.     if (right != nullptr) {
  47.         this->right = right;
  48.         this->right->setparent(this);
  49.     }
  50. }
  51.  
  52. Node::Node(const int& value, Node* parent, Node* left, const int& right) {
  53.     this->value = value;
  54.     this->parent = parent;
  55.     this->left = left;
  56.     this->right = new Node(right, this);
  57. }
  58.  
  59. Node::Node(const int& value, Node* parent, const int& left, Node* right) {
  60.     this->value = value;
  61.     this->parent = parent;
  62.     this->left = new Node(left, this);
  63.     this->right = right;
  64. }
  65.  
  66. Node::Node(const int& value, Node* parent, const int& left, const int& right) {
  67.     this->value = value;
  68.     this->parent = parent;
  69.     this->left = new Node(left, this);
  70.     this->right = new Node(right, this);
  71. }
  72.  
  73. Node::Node(const int& value, Node* left, const int& right) {
  74.     this->value = value;
  75.     this->left = left;
  76.     this->right = new Node(right, this);
  77. }
  78.  
  79. Node::Node(const int& value, const int& left, Node* right) {
  80.     this->value = value;
  81.     this->left = new Node(left, this);
  82.     if (right != nullptr) {
  83.         this->right = right;
  84.     }
  85. }
  86.  
  87. Node::Node(const int& value, const int& left, const int& right) {
  88.     this->value = value;
  89.     this->left = new Node(left, this);
  90.     this->right = new Node(right, this);
  91. }
  92.  
  93. Node::Node(const int& value, Node* parent) {
  94.     this->value = value;
  95.     if (parent != nullptr) {
  96.         this->parent = parent;
  97.     }
  98. }
  99.  
  100. Node::~Node() {
  101.     delete parent, left, right;
  102. }
  103.  
  104. void Node::setparent(Node* parent) {
  105.     this->parent = parent;
  106. }
  107.  
  108. void Node::setvalue(const int& value) {
  109.     this->value = value;
  110. }
  111.  
  112. Node& Node::getParent() const {
  113.     return *parent;
  114. }
  115.  
  116. Node& Node::getLeft() const {
  117.     return *left;
  118. }
  119.  
  120. Node& Node::getRight() const {
  121.     return *right;
  122. }
  123.  
  124. const int& Node::getValue() const {
  125.     return value;
  126. }
  127.  
  128. void output(const Node& binaryTree) {
  129.     std::cout << binaryTree.getValue() << std::endl;
  130.     if (&binaryTree.getLeft() != nullptr) {
  131.         output(binaryTree.getLeft());
  132.     }
  133.     if (&binaryTree.getRight() != nullptr) {
  134.         output(binaryTree.getRight());
  135.     }
  136. }
  137.  
  138. int main() {
  139.     Node binaryTree(1, new Node(10, 2, 11), new Node(8, new Node(3), 5));
  140.     output(binaryTree);
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement