Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 06-05-2009
Member
 
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:

Quote:
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
Reply With Quote
  #2  
Old 06-05-2009
Member
 
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
Reply With Quote
  #3  
Old 06-05-2009
Member
 
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++.
Reply With Quote
  #4  
Old 06-05-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
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 * /
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 03:48 PM.