Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8027453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:49:01+00:00 2026-06-04T23:49:01+00:00

I was trying out default argument values and function overloading in c++ by compiling

  • 0

I was trying out default argument values and function overloading in c++ by compiling the following code and I was surprised by the output which was :

Line 19: error: call of overloaded 'add()' is ambiguous

The code I compiled is :

#include <iostream>

using namespace std;

void add(int a=1, int b=1){

cout<<a+b;
}

void add(){

int a =2, b=2;
cout<<a+b;
}


int main(){

add();

return 0;
}

Any explanations why it is ambiguous? Thx in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T23:49:04+00:00Added an answer on June 4, 2026 at 11:49 pm

    Because both signatures match the call.

    add();
    

    can be interpreted as either add(1,1) or add(). When you write void add(int a=1, int b=1), you’re telling the compiler – “Listen dude, if I call add with no parameters, I want you to default them to 1“

    Most importantly, what do YOU expect to happen when you call add() with no parameters?

    • If you expect it to print 2, remove the version that takes no parameters.

    • If you expect it to print 4, remove the default parameters from the first version.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to figure out, what default values are used for the IP_MULTICAST_IF
I have the following html and js code snippets. Basically, I'm trying out Ember's
With the below javascript code i am trying some BackboneJs concepts. Couldn't figure out
I'm trying out Ninject and I'm modifying to code I wrote with Structure Map
I'm writing some simple point code while trying out Visual Studio 10 (Beta 2),
I am trying to set some default values for an object using after_initialize .
I am just trying out django and following the documentation for authentication. Basically I
I'm trying out the module-based architecture for my application, where the default module is
Trying out the examples found on pleac.sf.net , I'm not able to get an
Im trying out an example with EJB Session Beans. I wanna see the differences

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.