Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7869653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:23:50+00:00 2026-06-03T01:23:50+00:00

###MyClass.h### #ifndef _MyClass #define _MyClass #include <string> using namespace std; class MyClass { public:

  • 0
###MyClass.h###

    #ifndef _MyClass
    #define _MyClass
    #include <string>
    using namespace std;


    class MyClass
    {
    public:
        MyClass(const string name, const string text);
        void display(ostream & out) const;

        MyClass & operator = (const MyClass & m);
        int compare(const MyClass & right) const;

    private:
        string _name;
        string _text;
    };

    bool operator < (const MyClass & left, const MyClass & right);
    ostream & operator << (ostream & out, const MyClass & mc);
    #endif

###Node.h###

    #include <string>
    #include "MyClass.h"
    using namespace std;
    typedef MyClass * DataType; 

    class Node
    {
    private:
        DataType item; // data
        Node * lchild; // left child pointer
        Node * rchild; // right child pointer

    public:
        Node(DataType Item);
        DataType getItem() const;
        void setItem(const DataType  & data);
        Node* getLChild() const;
        void setLChild(Node * p);
        Node* getRChild() const;
        void setRChild(Node * p);
        virtual ~Node();
    };

###BST.h###

    #include "Node.h"
    using namespace std;

    class BST
    {
    private:
            Node * root; 
            bool Search(const DataType item, Node * r) const; 
            void Insert (DataType item, Node * ptr);
            void Destructor(const Node * r);

    public:
        BST();
        bool IsEmpty() const;
        void Insert(const DataType item);
        bool Search(const DataType item) const;
        virtual ~BST();
    };

###MyClass.cpp###

    #include <iostream>
    #include "MyClass.h"
    using namespace std;

    MyClass::MyClass(const string name, const string text)
    {
        _name = name;
        _text = text;
    }

    void MyClass::display(ostream & out) const
    {
        out << "Name: " << _name << endl;
        out << "Text: " << _text << endl;
    }

    MyClass & MyClass::operator = (const MyClass & m)  
    {  
        if (this == & m)
            return *this;

        _name = m._name;
        _text = m._text;

        return *this; 
    }

    int MyClass::compare(const MyClass & right) const
    {
        return _name.compare(right._name);
    }

    bool operator < (const MyClass & left, const MyClass & right)  
    {  
        return left.compare(right) > 0;
    }

    ostream & operator << (ostream & out, const MyClass & mc)
    {
        mc.display(out);
        return out;
    }

###Node.cpp###

    #include "Node.h"

    Node::Node(DataType Item):item(Item)
    {
        lchild = 0;
        rchild = 0;
    }

    DataType Node::getItem() const
    {
        DataType anItem = item; 
        return anItem;
    }

    void Node::setItem( const DataType & data)
    {
        item = data;
    }

    Node* Node::getLChild() const
    {
        Node * p = lchild;
        return p;
    }

    void Node::setLChild(Node * p)
    {
        lchild = p;
    }

    Node* Node::getRChild() const
    {
        Node * p = rchild;
        return p;
    }

    void Node::setRChild(Node * p)
    {
        rchild = p;
    }

    Node::~Node()
    {
    }

###BST.cpp###

    #include <iostream>
    #include "BST.h"
    using namespace std;

    bool BST::Search(const DataType item) const
    {
        return Search(item, root);
    }

    bool BST::Search(const DataType item, Node * r) const
    {
        if(r != 0)
        {
            if (item == r->getItem())
                return true;
            else
            {
                if (item < r->getItem())
                    return Search(item, r->getLChild());
                else
                    return Search(item, r->getRChild());
            }
        }
        else
            return false;
    }

    BST::BST()
    {
        root = 0;
    }

    bool BST::IsEmpty() const
    {
        return (root == 0);
    }

    void BST::Insert(const DataType item)
    {
        if(root == 0)
            root = new Node(item);
        else
            Insert(item, root);
    }

    void BST::Insert(DataType item, Node * ptr)
    {
        if (item < ptr->getItem())
        {
            if (ptr->getLChild() == 0)
                ptr->setLChild(new Node(item));
            else 
                Insert(item, ptr->getLChild());
        }
        else 
        {
            if (ptr->getRChild() == 0)
                ptr->setRChild(new Node(item));
            else 
                Insert(item, ptr->getRChild());
        }
    } 

    void BST::Destructor(const Node * r)
    {
        if(r!=0)
        {
            Destructor( r->getLChild());
            Destructor( r->getRChild());
            delete r;
        }
    }

    BST::~BST()
    {
        Destructor(root);
    }

###main.cpp###

    #include <iostream>
    #include "MyClass.h"
    #include "BST.h"
    using namespace std;

    void main()
    {
        MyClass * mc1 = new MyClass("Tree","This is a tree");
        MyClass * mc2 = new MyClass("Book","This is a book");
            MyClass * mc3 = new MyClass("Zoo","This is a zoo");

        BST * tree = new BST();
        tree->Insert(mc1);
        tree->Insert(mc2);
        tree->Insert(mc3);

        cout << boolalpha << ("Book" < "Tree") << endl;
        cout << (mc2 < mc1) << endl;

        cout << (tree->Search(new MyClass("Book",""))) << endl;
    }

Result is true false false

  1. I don’t know what’s wrong with my operator overloading? (mc2 should
    less than mc1)
  2. I’m not sure if this is correct for searching a “MyClass” node in a BST?
    and the result is “not found”….I traced it into “BST.cpp”,
    and found that the problem also occurs at ” if (item < r->getItem()) “

Can anyone help me or give me a hint….thank you so much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T01:23:51+00:00Added an answer on June 3, 2026 at 1:23 am

    Here you are just comparing pointers, i.e memory addresses:

    cout << (mc2 < mc1) << endl;
    

    To compare the objects, you need to dereference the pointers:

    cout << ((*mc2) < (*mc1)) << endl;
    

    In your code snippet, there is no reason for mc1, mc2, etc. to be pointers, so you could avoid the problem by creating objects on the stack directly:

    MyClass mc1("Tree","This is a tree");
    

    and so on. I would even go further and say that you should only dynamically allocate objects with new if you really are sure you need to and have good reasons not to allocate automatically on the stack. And if you really must use dynamically allocated pointers, have a look at C++ smart pointers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've some problem with using templates: myclass.h #ifndef MYCLASS_H #define MYCLASS_H class myclass {
#ifndef _MY_OPENCLPLATFORM_ #define _MY_OPENCLPLATFORM_ #include OpenCL.h namespace my { class OpenCLPlatform { cl_platform_id mplatformID;
MyClass.java: package test; public class MyClass { public void myMethod(){ System.out.println(My Method Called); }
class MyClass { public: void method2() { static int i; ... } }; Will
// MyClass.h namespace MyNamespace { static const double GasConstant = 1.987; class MyClass {
suppose I have a header foo.h like this: #ifndef FOO_H #define FOO_H #include <string>
class myClass { public: void operator++() { // ++myInstance. } void operator++(int) { //
public class MyClass { public int Age; public int ID; } public void MyMethod()
Here is my class: #include <iostream> #include gameobject.h #include IXmlAssigner.h #ifndef CHARACTER_H #define CHARACTER_H
I have: car.cc #include car.h #include <iostream> using namespace std; extern C Car* create_object()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.