Thread: problem while overloading == for templates in dev c++

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    25

    problem while overloading == for templates in dev c++

    i want to make a program which compares int,char,float numbers ; but i want to use templates for implementing different data types and i have to overload == for them.the program was compiled with no errors it also accepts the i/p but it doesn't display any message after that.here's the source code:-
    Code:
    #include<iostream>
    #include<conio.h>
    using namespace std;
    
    template<class t>
    
    void operator==(t a,t b)
    { 
      if(a==b)
       cout<<"\n TRUE ";
      else
       cout<<"\n FALSE ";
    }
    
    int main()
    {
        int choice;
        int a,b;
        float c,d;
        char e,f;
        
        cout<<"\n 1.Integer \n 2.Float \n 3.Character ";
        cout<<"\n\n Enter your choice = ";
        cin>>choice;
        switch(choice)
        {
          case 1:
          cout<<"\n Enter the first number  = ";
          cin>>a;
          cout<<"\n Enter the second number = ";
          cin>>b;
          a==b;
          break;
        
          case 2:
          cout<<"\n Enter the first number  = ";
          cin>>c;
          cout<<"\n Enter the second number = ";
          cin>>d;
          c==d;
          break;
        
          case 3:
          cout<<"\n Enter the first character  = ";
          cin>>e;
          cout<<"\n Enter the second character = ";
          cin>>f;
          e==f;
          break;
        
          default:
          cout<<"\n Wrong chocie";
          break;
        }
        getch();
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well my usual expectation of overloading == is that it should return a boolean result (not void).

    Simply saying a == b; in the code is often recognised as having no side effect, so chances are the code is just optimised out.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    25
    so how to fix it??

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Change the return type of the operator to bool, add a "return (a == b);" statement to the operator, and don't perform any output.

    It is also not possible to overload ANY operators where both operands are basic types (basic types include char, int, float, pointers). The compiler will simply not invoke your template, and will fall back on how it does comparisons by default.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with templates
    By manasij7479 in forum C++ Programming
    Replies: 7
    Last Post: 10-05-2011, 07:08 PM
  2. problem with templates
    By valery in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2009, 03:12 PM
  3. Templates problem
    By jw232 in forum C++ Programming
    Replies: 8
    Last Post: 03-12-2009, 01:27 PM
  4. problem with templates
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 03-23-2004, 03:54 PM
  5. Problem with templates
    By Lazy Student in forum C++ Programming
    Replies: 3
    Last Post: 11-17-2002, 12:57 PM