473,480 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem with constructor overloading.

30 New Member
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?
May 15 '07 #1
4 2739
DeMan
1,806 Top Contributor
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)
May 15 '07 #2
gpraghuram
1,275 Recognized Expert Top Contributor
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
May 15 '07 #3
Ganon11
3,652 Recognized Expert Specialist
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.
May 15 '07 #4
Banfa
9,065 Recognized Expert Moderator Expert
The problem is, these 2 consturctors both have 1 argument that they provide default values for.
Nice spot Ganon :D
May 15 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

17
2293
by: Ashwin | last post by:
hi guys, i have overloaded the << operator.as shown below. ostream& operator<<(ostream &out, const student &a) { out<<a.idno; out<< " " ; // out<< a.name; out<< " " ; // out<< a.marks...
0
268
by: Ashwin | last post by:
FamilyNet International Newsgate From: "Ashwin" <ashwin.bevinje@gmail.com> Kai-Uwe Bux wrote: Thanks Kai-Uwe Bux. As you said the copy constructor is strudent(
22
3586
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
2
4392
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
0
340
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
0
7048
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7050
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7091
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6743
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6966
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5344
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2999
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.