I have a templated class set up like so:

template<typename T>
class treeNode
{
    
public:
    bool operator==(const treeNode<T>&) const;
    bool operator>(const treeNode<T>&) const;
    void print();

    T field;
    int key;
};

Using the > operator as an example of my problem, I have it overloaded like this:

template<typename T>
bool treeNode<T>::operator>(const treeNode<T>& node) const
{
    if(field > node.field) return true;
    else return false;
}

This operator works fine when I'm working with integers e.g. treeNode<int> nodeInt; However, sometimes I need to work with C-style strings (char arrays) and I'm struggling to find a way that I can overload the operator and have it work for both datatypes. In order for it to successfully compare the two C-style strings, I thought I'd have to use strcmp(); so I'm not sure how to implement this and still have it work correctly with other datatypes such as integers. Also note that I am forced to use C-style strings, and cannot substitute these with std::string.

Any help would greatly be appreciated.

i think u should do something like

class cstring
{
        char *str;
public:
        cstring(const char *str)
        {
                this->str = (char *)malloc(strlen(str)+1);
                strcpy(this->str, str);
        }
        cstring(const cstring &cs)
        {
                this->str = (char *)malloc(strlen(cs.str)+1);
                strcpy(this->str, cs.str);
        }
        bool operator>(const cstring &b)
        {
                if(strcmp(str, b.str)>0)
                        return true;
                return false;
        }

        /*some more members*/
};



/*use it as*/
treeNode<cstring> tree;

I don't think c++ allows u to overload any operator for 'char *'.
For overloading, the arguments must be either of class type or enumerated type(Am not sure what does it mean by enumerated type though).

I considered creating my own class for the C-style strings but I thought it would be too complex.

I found a way around it though. Turns out I needed to use template specialization. After reading through how to do that it works fine.

I found a way around it though. Turns out I needed to use template specialization. After reading through how to do that it works fine.

hey yes
You can do that.

  1. Below is a program written in order to compute something, there are few syntax errors, and one logical error in it. Correct them and trace the program, following the line numbering below, first for inputs: 8 and 12, then for inputs: 25 and 15, you can choose more inputs if you wish. In one sentence, discover what the program is computing?

    include <iost>

    using namespace std

    int main()
    {
    int firstInput, secondInput;

    1. cout >> “enter two integers “, endl;
    2. cin << firstInput;
    3. cin << secondInput;

    4. if (firstInput < secondInput)
      {

    5. temp = firstInput;
    6. firstInput = secondInput;
    7. secondInput = temp;
      }
    8. if (secondInput < 0)
      {

      1. cout << “The numbers should be positive, bye!”;
        }
    9. firstInput % secondInput = Remainder;
    10. while (Remainder != 0)
      {
    11. firstInput == secondInput;
    12. secondInput = Remainder;
    13. Remainder = firstInput % secondInput;
      }
    14. cout << “The number you are looking for is: << secondInput”;
    15. return 0;
      }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.