Results 1 to 4 of 4

Thread: Error in function overloading

  1. #1
    Join Date
    Nov 2008
    Posts
    1,022

    Error in function overloading

    I just started learning C++. Here is a code on the overloading of functions. My problem is I am unable to find the error. Can some one please help me on this?

    Code:
     # include <iostream> 
      # include <conio.h> 
    
      void test (int n = 0, float x = 2.5) 
      {
           cout << "Function No. 1:"; 
           cout << "n =" <<n << "x =" <<x << "\ n"; 
      }
    
      void test (float x = 4.1, int n = 2) 
      { 
        cout << "Function No. 2:"; 
        cout<< "n= " <<n<< " x = " <<x<< " \n " ;
      } 
      void main () 
      { 
           int i = 5; float r = 3.2; 
           test (i, r) // function N1 
           test (r, i) // function N2 
           test (i) // function N1 
           test (r) // function N2 
         
           }
    I get the error:

    18 C:\Users\Nas\Desktop\Projects\Exo 1.5\main.cpp `main' must return `int'
    C:\Users\Nas\Desktop\Projects\Exo 1.5\Makefile.win [Build Error] [main.o] Error 1

  2. #2
    Join Date
    May 2008
    Posts
    685

    Re: Error in function overloading

    The compiler gives you the answer. The main () must return an integer, so ...

    1 - you rename your main() into "int main ()"
    2 - you put a "return 0" at the end, if you do not want to return anything

  3. #3
    Join Date
    May 2008
    Posts
    945

    Re: Error in function overloading

    "int f ()" indicates that the function "f ()" returns an int value. That is why we need to add a return statement at the end (eg return 0; means that the function returns the integer zero)

    "void f ()" means that the function f () does not returns any value.

    However the main function is a little different because it is the entry point of the program. Some compilers requires that a C++ program should return a value, so that's why it is necessary that the function main () returns an int.

    Therefore the function "void main ()" is not legal in C++.

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Error in function overloading

    One thing that should be understandable initially is that overloading function only works if you have a different number or types of arguments.

    Thus, a surcharge may be in a form close to

    Code:
      void foo () 
      void foo (int) 
      void foo (int, int)
    but you can not consider overloading

    Code:
      void foo (int i = 3, int j = 4) 
     {
          / * What should be done * / 
     } 
      void foo (int i = 5, int j = 6)
    because the number and type of argument is identical.

    Remember that if you give default values for both arguments (as example), you find yourself in a reality with the option of using prototypes

    Code:
      void foo () / * the default values are used * / 
      void foo (int i) / * the default value of j is used * / 
      void foo (int i, int j) / * no default is used * /

Similar Threads

  1. What is constructor overloading in c++
    By Mast Maula in forum Software Development
    Replies: 4
    Last Post: 08-01-2011, 10:34 AM
  2. Method overloading c#.net
    By raju_srk in forum Software Development
    Replies: 1
    Last Post: 22-11-2010, 07:00 PM
  3. Function overloading with polymorphism
    By Mega Member in forum Software Development
    Replies: 3
    Last Post: 02-10-2009, 03:35 PM
  4. Error in Operator Overloading
    By DutchDude in forum Software Development
    Replies: 2
    Last Post: 13-05-2009, 11:54 PM
  5. overloading of operators in C++
    By shilong in forum Software Development
    Replies: 3
    Last Post: 28-01-2009, 10:30 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,717,392,019.69759 seconds with 16 queries