Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 21st, 2006, 7:51 AM   #1
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 164
Rep Power: 9 hbe02 is on a distinguished road
Multiple constructors

Im writing a class which i want to give 2 contructors, one constructor has input parameters and one doesnt. Is this the correct way of doing things.
I only recieve a warning:
Quote:
warning C4520: 'RTPpacket' : multiple default constructors specified
Is there something i should add or specify when writing a class with 2 constructors to avoid such warnings?
thanks.
hbe02 is offline   Reply With Quote
Old Apr 21st, 2006, 8:31 AM   #2
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 11 nnxion is on a distinguished road
I'm not sure when that occurs without seeing your code.
Here is an example of how it does work.
#include <iostream>
#include <string>

using namespace std;

class Packet
{
	private:
		int packetID;
		string packetName;
	public:
		Packet();
		Packet(int);
		Packet(int, string);
		void PrintAttributes();
};

Packet::Packet()
{
	packetID = 0;
	packetName = "no name";
}

Packet::Packet(int id)
{
	packetID = id;
	packetName = "no name";
}

Packet::Packet(int id, string str)
{
	packetID = id;
	packetName = str;
}

void Packet::PrintAttributes()
{
	cout << "PacketID = " << packetID << endl;
	cout << "PacketName = " << packetName << endl << endl;
}

int main()
{
	Packet packet1;
	packet1.PrintAttributes();

	Packet packet2(1);
	packet2.PrintAttributes();

	Packet packet3(2, "myPacket");
	packet3.PrintAttributes();

	return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 21st, 2006, 8:37 AM   #3
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,329
Rep Power: 11 grumpy will become famous soon enough
Some compilers will complain about multiple default constructors (or ambiguous function calls) in a circumstance like;
class Something
{
    public:

        Something();
        Something(int x = 0, int y = 0);
};
as both constructors can be invoked without arguments (i.e. both are default constructors).

It is more common for a compiler to complain when attempting to use the class in this case (as that's when the compiler realises there are two candidates that can be called), but some smarter (?) ones do actually complain when compiling the class declaration.

Another possible cause of such a message is accidentally providing two constructors that take no arguments;
class Something
{
    public:

        Something();

        //   and later
        Something();     // Whoops!
};
grumpy is offline   Reply With Quote
Old Apr 21st, 2006, 10:49 AM   #4
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 164
Rep Power: 9 hbe02 is on a distinguished road
Thanks for your help guys.
I recieved the warning because i defined the contructors inside the class. the warning no longer appeared when i only defined the contructors return type parameters in the class and wrote their implemented code outside. just like nnxion wrote it.
hbe02 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:45 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC