Problem with constructor overloading.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sandeepkavade
    New Member
    • Mar 2007
    • 30

    #1

    Problem with constructor overloading.

    hi all,
    i am having a class and i want to impliment 2 overloaded constructors as:
    in .h file:
    Document(const char* filename=NULL);
    Document(const wchar_t* filename=NULL);

    in .cpp file

    Document:: Document( const char* filespec )
    {
    m_pdoc = NULL;
    load( filespec );
    }

    Document:: Document( const wchar_t* filespec )
    {
    m_pdoc = NULL;
    load( filespec );
    }

    the load function is also overloaded.

    but it gives me warning saying:
    warning C4520: 'Document' : multiple default constructors specified

    and error saying:

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

    where i am going wrong?
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    I suspect (but could be horribly wrong) that this is because pointers are all much the same so a constructor that takes a pointer to one thing is too similar to a constructor that takes a pointer to another thing to be able to distinguish between them.
    You could use a macro for the constructors input to do the same thing (and the macro could be used to decide whether we are using unicode or not)

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      Hi,
      There i no problem with the overloading.
      But the problem is with the way u call the Constructor.
      Provide the snippet where u call the constructor, because if the argument to the constructor is proper then u wont get any error.

      Thanks
      Raghuram

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        The problem is, these 2 consturctors both have 1 argument that they provide default values for. Assuming there was only 1 constructor like this, it would work fine. You could pass it either a char* or nothing, and it would still work. In the second case, it treats the char* as being NULL. Now, you add this second constructor, which is just fine. Now if a wchar_t* is passed, your program can deal with it - but you also provide a default value for this.

        Now, you call the constructor with no arguments. Which one is called? Both could work since they both provide default values for the arguments. You should only have 1 constructor with default values.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by Ganon11
          The problem is, these 2 consturctors both have 1 argument that they provide default values for.
          Nice spot Ganon :D

          Comment

          Working...