二进制搜索树<<操作员超载不起作用

时间:2016-11-22 23:16:00

标签: c++ class linked-list operator-overloading binary-search-tree

我有3个类,可以创建一个完整的二进制搜索树。这3个班级是 1. DBentry(存储名称,IP地址和状态), 2. TreeNode(指向其自己的DBentry,以及其左侧和右侧的条目) 3. TreeDB(包含根TreeNode并提供各种函数来添加,删除,更新和查找DBentryobjects)

在DBentry里面我有friend ostream& operator <<(ostream& out, const DBentry& rhs); 在TreeDB里面我有friend ostream& operator<< (ostream& out, const TreeDB& rhs); friend ostream& operator <<(ostream& out, TreeNode* rhs); 这些重载运算符似乎无法正常工作。任何帮助都会非常有用。

Class DBentry:

class DBentry {
private:
string name;
unsigned int IPaddress;
    bool active;

public:

DBentry();
    DBentry (string _name, unsigned int _IPaddress, bool _active);


~DBentry(); 


void setName(string _name);


void setIPaddress(unsigned int _IPaddress);


    void setActive (bool _active);


string getName() const;


unsigned int getIPaddress() const;


    bool getActive() const;

    friend ostream& operator <<(ostream& out, const DBentry& rhs);
};

类TreeNode:

class TreeNode {
private:
DBentry* entryPtr;
TreeNode* left;
TreeNode* right;

public:
TreeNode();

TreeNode(DBentry* _entryPtr);


~TreeNode();


void setLeft(TreeNode* newLeft);


void setRight(TreeNode* newRight);


TreeNode* getLeft();


TreeNode* getRight();


DBentry* getEntry() const;

bool find(string _name);


};

类TreeDB有private: TreeNode* root;

ostream& operator <<(ostream& out, const DBentry& rhs){
out<<rhs.name<<" : "<<rhs.IPaddress<<" : ";//<<rhs.active?    (out<<"active"):(out<<"inactive")<<endl;
if(rhs.active)
    out<<"active";
else
    out<<"inactive";
out<<endl;
}

ostream& operator <<(ostream& out, TreeNode& rhs){
if(rhs.getEntry()!=NULL){
    out << *(rhs.getLeft());
    out << *(rhs.getEntry());
    out << *(rhs.getRight());
}
}

ostream& operator<< (ostream& out, const TreeDB& rhs){
out << *(rhs.root);
}

1 个答案:

答案 0 :(得分:2)

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { if let presentedController = window?.rootViewController?.presentedViewController, let avFullScreen = NSClassFromString("AVFullScreenViewController").self, let mpInlineVideoFullscreen = NSClassFromString("MPInlineVideoFullscreenViewController").self { if presentedController.isKind(of: MPMoviePlayerViewController.self) || presentedController.isKind(of: avFullScreen) || presentedController.isKind(of: mpInlineVideoFullscreen) { return UIInterfaceOrientationMask.allButUpsideDown } } return UIInterfaceOrientationMask.portrait } 表示该函数返回对ostream& operator <<(ostream& out, TreeNode& rhs)的引用。代码不会返回ostream引用,因此程序将继续进行未定义行为的快乐冒险。

至少,在程序的未发布部分可能存在其他问题,OP必须

ostream

其他ostream& operator <<(ostream& out, TreeNode& rhs){ if(rhs.getEntry()!=NULL){ out << *(rhs.getLeft()); out << *(rhs.getEntry()); out << *(rhs.getRight()); } return out; //<-- return the stream. Do not cross streams unless fighting Gozer. } 重载具有相同的缺陷。