error C2668: 'Name::Name' : ambiguous call to overloaded function

Visual studio 2008 is being ridiculous.. Can you please assist with this error? It's telling me it doesn't know which constructor to call for my base class Name, when I define the default constructor for my derived class, derivedName..


Here is the code:



1
2
3
4
5
6
7
8
derivedName::derivedName() : Name()  //whether I put :Name() or not, I receive the same error 

{
// error C2668: 'Name::Name' : ambiguous call to overloaded function

lastName = ""; 

}


The only constructors for Name are as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Name::Name(std::string firstName = "")
{

	firstName = firstName;

}

Name::Name()
{

	firstName = "";

}


Name::~Name(){/*** I'm the destructor ***/}


I can't get rid of this code.. and if I comment out any constructor/destructors I receive token errors for 'dynamic constructor/destructor' s
I'm sorry if it's a stupid question, but why do you have

Name::Name(std::string firstName = "")

instead of

Name::Name(std::string firstName)

?
Last edited on
You have two default constructors. The first is one due to the default value for the parameter and nothing needs to be said about the second one.
As far as my understanding goes, it allows you to default the firstName parameter to "" if you do not include a parameter...

soo.... it probably generates a constructor for Name(std::string) and thats where the duplicate is..
Wow thank you both so much.. that was really quick and really ignorant of me!! hah =D

Edit to prevent multiple posts:

In this scenario.. is it better to do Name::Name(std::string firstName = "") or
1
2
Name::Name(std::string firstName)
Name::Name()
Last edited on
IMO, Name::Name(std::string firstName = "") is better since the other approach is redundant.
I was thinking the same thing, wasn't sure if there was much of a difference or not. Thanks!
IMO two different constructors is better.

If you ever want to add a second parameter to the constructor, then you have to either
put it first in the parameter list or default it as well. By defaulting it, it means that any
code that currently uses the single parameter constructor continues to compile using the
default value for the second parameter, which may not be what you want. By not
defaulting either, the compiler will catch every place you need to update in your code
(compile error) for you. Less chance of coding bug.
By defaulting it, it means that any
code that currently uses the single parameter constructor continues to compile using the
default value for the second parameter


If/when that situation comes up, then I'd write a seperate ctor. As it stands now, both ctors construct the object the exact same way so writing two just means more code and more maintanance.

A more common bug that's easier to make would be when you add more member variables to the class -- you'll probably remember to initialize them in one constructor but might forget to do it in all your constructors if you have multiple.

Duplicate code is way worse, IMO. At least when you need to change the ctor interface you actually have to look at your ctors and how changes to them will impact your code.
Topic archived. No new replies allowed.