C++ C++;:操作员<&书信电报;嵌套类中的重载

C++ C++;:操作员<&书信电报;嵌套类中的重载,c++,c++11,operator-overloading,binary-tree,nested-class,C++,C++11,Operator Overloading,Binary Tree,Nested Class,这个问题在这里有一个详细的答案: 我试图重载一个嵌套的子类,并花了一个小时试图重载operatorleft=newnode;/添加一个新的左子对象 v->左->标准杆=v;//v是它的父母 v->right=新节点;//和一个新的正确的孩子 v->右->标准杆=v;//v是它的父母 n+=2;//另外两个节点 } 模板 typename LinkedBinaryTree::Position//删除p和父级 LinkedBinaryTree::removeAboveExternal(常量位置和p)

这个问题在这里有一个详细的答案:

我试图重载一个嵌套的子类,并花了一个小时试图重载
operatorleft=newnode;/添加一个新的左子对象
v->左->标准杆=v;//v是它的父母
v->right=新节点;//和一个新的正确的孩子
v->右->标准杆=v;//v是它的父母
n+=2;//另外两个节点
}
模板
typename LinkedBinaryTree::Position//删除p和父级
LinkedBinaryTree::removeAboveExternal(常量位置和p){
Node*w=p.v;Node*v=w->par;//获取p的节点和父节点
节点*sib=(w==v->左?v->右:v->左);
如果(v==\u根){//根的子级?
_root=sib;//…生成同级根
sib->par=NULL;
}
否则{
Node*gpar=v->par;//w的祖父母
如果(v==gpar->left)gpar->left=sib;//用sib替换父级
else gpar->right=sib;
sib->par=gpar;
}
删除w;删除v;//删除删除的节点
n-=2;//少两个节点
返回位置(sib);
}
//前序遍历
模板
void LinkedBinaryTree::预订单(节点*v、位置列表和pl)常量{
pl.push_back(位置(v));//添加此节点
if(v->left!=NULL)//遍历左子树
预订单(v->左,pl);
if(v->right!=NULL)//遍历右子树
预订单(v->右,pl);
}
模板

std::ostream&operator读了一整天之后,我觉得我找到了接近解决方案的东西。我听取了他的建议,并将声明内联。除此之外,我还必须将我的GCC更新为4.9(之前使用的是4.2.1),他们改变了它的行为方式(有点)。无论如何,嵌套类的最佳解决方案似乎是内联定义。固定代码如下

p_7_1.cpp:

#include "tree.hpp"

typedef LinkedBinaryTree<int> Tree;


#include <iostream>

using namespace std;


int main() {
  // Test if tree works:
  Tree lbt;
  cout << lbt.empty() << endl;
  lbt.addRoot();
  lbt.addRoot();
  cout << lbt.empty() << endl;
  cout << lbt.size() << endl;
  lbt.expandExternal(lbt.root());
  cout << lbt.empty() << endl;
  cout << lbt.size() << endl;

  *(lbt.root()) = 12;
  cout << lbt;
}
#include "tree.hpp"

typedef LinkedBinaryTree<int> Tree;

#include <iostream>

using namespace std;

int main() {
  // Test if tree works:
  Tree lbt;
  cout << lbt.empty() << endl;
  lbt.addRoot();
  lbt.addRoot();
  cout << lbt.empty() << endl;
  cout << lbt.size() << endl;
  lbt.expandExternal(lbt.root());
  cout << lbt.empty() << endl;
  cout << lbt.size() << endl;

  // rotateLeft(lbt.root().right());
  *(lbt.root()) = 12;
  *(lbt.root().left()) = 11;
  *(lbt.root().right()) = 13;
  cout << lbt;
}
#包括“tree.hpp”
typedef链接二叉树;
#包括
使用名称空间std;
int main(){
//测试树是否工作:
树lbt;

如果我在类:
classa{classb]中声明友元操作符,我能让它工作吗{friend std::ostream&operator使用-Wall编译并实际阅读您的警告如果您阅读问题的开头,您将看到我使用-Wall编译它,并且我也提供了错误。并且没有警告。您实际上应该阅读问题。如果您再次阅读我的评论,您将看到您需要阅读您的警告,而不仅仅是启用它们。您在问题中提到过您有警告吗?如果没有,为什么没有?请注意,此声明
template std::ostream&operator哦,我认为我必须声明它,因为我在
std::ostream&operator中使用它。您没有使用此声明。它永远不会在重载中被拾取解析。尝试删除内联
运算符
#ifndef LINKED_BINARY_TREE_HPP
#define LINKED_BINARY_TREE_HPP

#include <list>

template <typename T> class LinkedBinaryTree;
template <typename T> std::ostream& operator<<(std::ostream& os, const LinkedBinaryTree<T>& lbt);
template <typename T> std::ostream& operator<<(std::ostream& os, const typename LinkedBinaryTree<T>::Position& p);

template <typename T>
class LinkedBinaryTree {
protected:
  struct Node {         // a node of the tree
    T    elt;       // element value
    Node*   par;        // parent
    Node*   left;       // left child
    Node*   right;      // right child
    Node() : elt(), par(NULL), left(NULL), right(NULL) { } // constructor
  };

public:
  class Position {      // position in the tree
  private:          // 
    Node* v;            // pointer to the node
  public:
    Position(Node* _v = NULL) : v(_v) { } // constructor
    T& operator*()            // get element
    { return v->elt; }            // 
    Position left() const         // get left child
    { return Position(v->left); }     // 
    Position right() const        // get right child
    { return Position(v->right); }    // 
    Position parent() const       // get parent
    { return Position(v->par); }      // 
    bool isRoot() const           // root of the tree?
    { return v->par == NULL; }        // 
    bool isExternal() const       // an external node?
    { return v->left == NULL && v->right == NULL; } // 
    friend class LinkedBinaryTree; // give tree access
  public:
    friend std::ostream& operator<<(std::ostream& os, const typename LinkedBinaryTree<T>::Position& p);
    friend std::ostream& operator<< <T>(std::ostream& os, const LinkedBinaryTree<T>& lbt);
  };
  typedef std::list<Position> PositionList; // list of positions
public:                     // 
  LinkedBinaryTree();               // constructor
  int size() const;             // number of nodes
  bool empty() const;               // is tree empty?
  Position root() const;            // get the root
  PositionList positions() const;       // list of nodes
  void addRoot();               // add root to empty tree
  void expandExternal(const Position& p);   // expand external node
  Position removeAboveExternal(const Position& p); // remove p and parent
                // housekeeping functions omitted...
protected:          // local utilities
  void preorder(Node* v, PositionList& pl) const; // preorder utility
public:
  friend std::ostream& operator<< <T>(std::ostream& os, const LinkedBinaryTree<T>& lbt);
private:                      // 
  Node* _root;          // pointer to the root
  int n;            // number of nodes
};              // 

template <typename T>
LinkedBinaryTree<T>::LinkedBinaryTree() // constructor
  : _root(NULL), n(0) { }

template <typename T>
int LinkedBinaryTree<T>::size() const // number of nodes
{ return n; }

template <typename T>
bool LinkedBinaryTree<T>::empty() const // is tree empty?
{ return size() == 0; }

template <typename T>
typename LinkedBinaryTree<T>::Position LinkedBinaryTree<T>::root() const // get the root
{ return Position(_root); }

template <typename T>
typename LinkedBinaryTree<T>::PositionList LinkedBinaryTree<T>::positions() const {
  PositionList pl;
  preorder(_root, pl);      // preorder traversal
  return PositionList(pl);  // return resulting list
}

template <typename T>
void LinkedBinaryTree<T>::addRoot() // add root to empty tree
{ _root = new Node; n = 1; }

template <typename T>
void LinkedBinaryTree<T>::expandExternal(const Position& p) {
  Node* v = p.v;        // p's node
  v->left = new Node;       // add a new left child
  v->left->par = v;     // v is its parent
  v->right = new Node;      // and a new right child
  v->right->par = v;        // v is its parent
  n += 2;           // two more nodes
}

template <typename T>
typename LinkedBinaryTree<T>::Position  // remove p and parent
LinkedBinaryTree<T>::removeAboveExternal(const Position& p) {
  Node* w = p.v;  Node* v = w->par; // get p's node and parent
  Node* sib = (w == v->left ?  v->right : v->left);
  if (v == _root) {     // child of root?
    _root = sib;        // ...make sibling root
    sib->par = NULL;
  }
  else {
    Node* gpar = v->par;           // w's grandparent
    if (v == gpar->left) gpar->left = sib; // replace parent by sib
    else gpar->right = sib;
    sib->par = gpar;
  }
  delete w; delete v;       // delete removed nodes
  n -= 2;           // two fewer nodes
  return Position(sib);
}

// preorder traversal
template <typename T>
void LinkedBinaryTree<T>::preorder(Node* v, PositionList& pl) const {
  pl.push_back(Position(v));    // add this node
  if (v->left != NULL)      // traverse left subtree
    preorder(v->left, pl);
  if (v->right != NULL)     // traverse right subtree
    preorder(v->right, pl);
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const LinkedBinaryTree<T>& lbt){
  os << lbt.root();
  // os << *(lbt.root());
  return os;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const typename  LinkedBinaryTree<T>::Position& p) {
  os << *p;         // Other func stuff will be here later
  return os;
}

#endif
#include "tree.hpp"

typedef LinkedBinaryTree<int> Tree;

#include <iostream>

using namespace std;

int main() {
  // Test if tree works:
  Tree lbt;
  cout << lbt.empty() << endl;
  lbt.addRoot();
  lbt.addRoot();
  cout << lbt.empty() << endl;
  cout << lbt.size() << endl;
  lbt.expandExternal(lbt.root());
  cout << lbt.empty() << endl;
  cout << lbt.size() << endl;

  // rotateLeft(lbt.root().right());
  *(lbt.root()) = 12;
  *(lbt.root().left()) = 11;
  *(lbt.root().right()) = 13;
  cout << lbt;
}
#ifndef LINKED_BINARY_TREE_HPP
#define LINKED_BINARY_TREE_HPP

#include <cstdlib>
#include <iostream>
#include <list>

template <typename T> class LinkedBinaryTree;
template <typename T> std::ostream& operator<<(std::ostream& os, const LinkedBinaryTree<T>& lbt);
template <typename T> std::ostream& operator<<(std::ostream& os, const typename LinkedBinaryTree<T>::Position& p);

template <typename T>
class LinkedBinaryTree {
protected:
  struct Node {         // a node of the tree
    T    elt;       // element value
    Node*   par;        // parent
    Node*   left;       // left child
    Node*   right;      // right child
    Node() : elt(), par(NULL), left(NULL), right(NULL) { } // constructor
  };

public:
  class Position {      // position in the tree
  private:          // 
    Node* v;            // pointer to the node
  public:
    Position(Node* _v = NULL) : v(_v) { } // constructor
    T& operator*()            // get element
    { return v->elt; }            // 
    Position left() const         // get left child
    { return Position(v->left); }     // 
    Position right() const        // get right child
    { return Position(v->right); }    // 
    Position parent() const       // get parent
    { return Position(v->par); }      // 
    bool isRoot() const           // root of the tree?
    { return v->par == NULL; }        // 
    bool isExternal() const       // an external node?
    { return v->left == NULL && v->right == NULL; } // 
    friend class LinkedBinaryTree; // give tree access
  public:
    //friend std::ostream& operator<< <T> (std::ostream& os, const LinkedBinaryTree<T>::Position& p);
    friend inline std::ostream& operator<<(std::ostream& os, const Position& p) {
      os << '[';
      if (!p.isExternal()){
    os << p.left();
      }
      os << ' ';
      os << *(Position(p));
      os << ' ';
      if (!p.isExternal()) {
    os << p.right();
      }
      os << ']';

      return os;
    }

    friend std::ostream& operator<< <T>(std::ostream& os, const LinkedBinaryTree<T>& lbt);
  };
  typedef std::list<Position> PositionList; // list of positions
public:                     // 
  LinkedBinaryTree();               // constructor
  int size() const;             // number of nodes
  bool empty() const;               // is tree empty?
  Position root() const;            // get the root
  PositionList positions() const;       // list of nodes
  void addRoot();               // add root to empty tree
  void expandExternal(const Position& p);   // expand external node
  Position removeAboveExternal(const Position& p); // remove p and parent
                // housekeeping functions omitted...
protected:          // local utilities
  void preorder(Node* v, PositionList& pl) const; // preorder utility
public:
  friend std::ostream& operator<< <T>(std::ostream& os, const LinkedBinaryTree<T>& lbt);
private:                      // 
  Node* _root;          // pointer to the root
  int n;            // number of nodes
};              // 

template <typename T>
LinkedBinaryTree<T>::LinkedBinaryTree() // constructor
  : _root(NULL), n(0) { }

template <typename T>
int LinkedBinaryTree<T>::size() const // number of nodes
{ return n; }

template <typename T>
bool LinkedBinaryTree<T>::empty() const // is tree empty?
{ return size() == 0; }

template <typename T>
typename LinkedBinaryTree<T>::Position LinkedBinaryTree<T>::root() const // get the root
{ return Position(_root); }

template <typename T>
typename LinkedBinaryTree<T>::PositionList LinkedBinaryTree<T>::positions() const {
  PositionList pl;
  preorder(_root, pl);      // preorder traversal
  return PositionList(pl);  // return resulting list
}

template <typename T>
void LinkedBinaryTree<T>::addRoot() // add root to empty tree
{ _root = new Node; n = 1; }

template <typename T>
void LinkedBinaryTree<T>::expandExternal(const Position& p) {
  Node* v = p.v;        // p's node
  v->left = new Node;       // add a new left child
  v->left->par = v;     // v is its parent
  v->right = new Node;      // and a new right child
  v->right->par = v;        // v is its parent
  n += 2;           // two more nodes
}

template <typename T>
typename LinkedBinaryTree<T>::Position  // remove p and parent
LinkedBinaryTree<T>::removeAboveExternal(const Position& p) {
  Node* w = p.v;  Node* v = w->par; // get p's node and parent
  Node* sib = (w == v->left ?  v->right : v->left);
  if (v == _root) {     // child of root?
    _root = sib;        // ...make sibling root
    sib->par = NULL;
  }
  else {
    Node* gpar = v->par;           // w's grandparent
    if (v == gpar->left) gpar->left = sib; // replace parent by sib
    else gpar->right = sib;
    sib->par = gpar;
  }
  delete w; delete v;       // delete removed nodes
  n -= 2;           // two fewer nodes
  return Position(sib);
}

// preorder traversal
template <typename T>
void LinkedBinaryTree<T>::preorder(Node* v, PositionList& pl) const {
  pl.push_back(Position(v));    // add this node
  if (v->left != NULL)      // traverse left subtree
    preorder(v->left, pl);
  if (v->right != NULL)     // traverse right subtree
    preorder(v->right, pl);
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const LinkedBinaryTree<T>& lbt){
  os << lbt.root();
  os << std::endl;
  // os << *(lbt.root());
  return os;
}

/*
template <typename T>
std::ostream& operator<<(std::ostream& os, const typename LinkedBinaryTree<T>::Position& p) {
  os << '[';
  if (!p.isExternal()){
    os << p.left();
  }
  os << ' ';
  os << *(Position(p));
  os << ' ';
  if (!p.isExternal()) {
    os << p.right();
  }
  os << ']';

  return os;
}
*/
#endif