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

This is a discussion on problem while overloading == for templates in dev c++ within the C++ Programming forums, part of the General Programming Boards category; i want to make a program which compares int,char,float numbers ; but i want to use templates for implementing different ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,205
    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%.

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, 02:54 PM
  5. Problem with templates
    By Lazy Student in forum C++ Programming
    Replies: 3
    Last Post: 11-17-2002, 11:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21