Thread: Overloading the "abs" function

  1. #1
    Learning the art of C++
    Join Date
    Aug 2005
    Posts
    7

    Overloading the "abs" function

    Well, to start off, I am attempting to do one of the programming projects in Problem Solving with C++ by Walter Savitch. The program asks me to overload the "abs" function using the "long", "int", "float", and "double" types, which, I try to do here:

    Code:
     #include<iostream>
    #include<cstdlib>
    using namespace std;
    
    int abs(int i); // Function Definitions
    double abs(double d);
    float abs(float f);
    long abs(long l);
    
    int main()
    {
        int i = 1;
        double d = 2.0;
        float f = 3.0;
        long l = -4;
        cout << "Overloading Functions\n";
        cout << "Int: " << abs(i) << endl;
        cout << "Float: " << abs(d) << endl;
        cout << "Double: " << abs(f) << endl;
        cout << "Long: " << abs(l) << endl;
        
        system("PAUSE");
        return 0;
    }
    With the errors being as follows:

    In function `int main()':
    note :5 candidates are: int abs(int)
    20 call of overloaded `abs(long int&)' is ambiguous
    note double abs(double)
    note float abs(float)
    note long int abs(long int)
    note long long int __gnu_cxx::abs(long long int)
    note long int std::abs(long int)

    Using latest beta of DevC++...not sure if I am doing something blatantly obvious as far as overloading goes, or maybe I just misunderstood the concept...but from the tutorials and information I have read...it looks right, but alas, I am not sure what's wrong. Any help would be greatly appreciated...my goal is to have a good understanding of c++.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Namespace issue? If you actually defined these functions but used this
    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    instead of this
    Code:
    #include<iostream>
    using namespace std;
    would the problem persist?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There are some functions in the std namespace named abs. You're probably clashing with them.

  4. #4
    Learning the art of C++
    Join Date
    Aug 2005
    Posts
    7
    Namespace issue? If you actually defined these functions but used this
    Code:
    #include <iostream>
    using std::cout;
    using std::endl;instead of this
    Code:
    #include<iostream>
    using namespace std;would the problem persist?
    It turns out, that I had to do it this way. At first I was under the impression that I had to overload the predefined function "abs" to work with long, double, and float. But it turns out, I had to use my own written function of "abs" and overload it that way. Thanks for the replies, it helped me understand what my problem was.

  5. #5
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Code:
    #include<iostream>
    #include<cstdlib>
    using namespace std;
    
    template <typename T>
    T abs1(T i)
    {
    	return abs(i);
    }
    
    int main()
    {
        int i = 1;
        double d = 2.0;
        float f = 3.0;
        long l = -4;
        cout << "Overloading Functions\n";
        cout << "Int: " << abs1(i) << endl;
        cout << "Float: " << abs1(d) << endl;
        cout << "Double: " << abs1(f) << endl;
        cout << "Long: " << abs1(l) << endl;
        
        system("PAUSE");
        return 0;
    }

  6. #6
    Learning the art of C++
    Join Date
    Aug 2005
    Posts
    7
    Hmmm, this is only chapter three of the book, talking about functions and function calls, templates aren't until later in the book, but still cool nonetheless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  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: 5
    Last Post: 02-08-2003, 07:42 PM