Thread: function overloading

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    function overloading

    When I compile the included code with the Borland c++ 5.5 command line tools I get the error:
    C:\BC45\bob>bcc32 over1.cpp
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    OVER1.CPP:
    Error E2015 OVER1.CPP 13: Ambiguity between 'std::abs(int)' and 'abs(int)' in fu
    nction main()
    Warning W8004 OVER1.CPP 20: 'i' is assigned a value that is never used in functi
    on main()
    *** 1 errors in Compile ***
    Why?

    Also, when I compile it with Borland C++ 4.5 it rounds 64.53 upto 65 when instead it should output 64.53. Isn't setprecision() used for specifying how many decimal places you want? Like in C you could say printf("%.2f");.


    Code:
    #include <iostream>
    #include <iomanip.h>
    
    int abs(int i);
    float abs(float x);
    int main()
    {
       int ians;
       float fans;
       int i = -15;
       float x = -64.53;
    
       ians = abs(i);
       cout << "Integer absolute value of -15 is " << ians << endl;
    
       fans = abs(x);
       cout << "Float absolute value of -64.53 is " << setprecision(2) << fans << endl;
       cin.get();
       return 0;
    }
    
    int abs(int i)
    {
       if(i < 0)
          return(i * -1);
       else
          return (i);
    }
    
    float abs(float x)
    {
       if(x < 0.0)
          return(x * -1.00);
       else
          return (x);
    }

  2. #2
    put a "using namespace std;" in there under the headers


    Also, naming a function the same as an STD fxn is not a good idea - create a namespace around your functions and everything should be good.

    Code:
    namespace myMath{
    int abs(int i);
    float abs(float x);
    }
    
    .......
    
    int myMath::abs(int i)
    {
       if(i < 0)
          return(i * -1);
       else
          return (i);
    }
    
    float myMath::abs(float x)
    {
       if(x < 0.0)
          return(x * -1.00);
       else
          return (x);
    }
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Or...if you want to use your functions glabally, add the global namespace specifire when you call

    ians = ::abs(i);

  4. #4
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    Instead all this, just stick to the old standard and put:

    Code:
    #include<iostream.h>   // with .h ending
    also get rid of the line

    Code:
    precision(2);
    and to get rid of all the warnings use 'double' instead of 'float' to avoid any data loss, i.e. (0.0 is a 'double')


    good luck

    matheo917

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    iostream.h is like you said, old. it will produce warnings on newer compilers, just mentioning if you decide to use it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post your games here...
    By Hammer in forum Game Programming
    Replies: 132
    Last Post: 02-28-2013, 09:29 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM