473,394 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

compilation error with ctor

Consider the following program:

#include <iostream>
#include <string>

class Member {
int x;
int y;
public:
Member(int argx, int argy);
Member(const Member &m);
void operator=(const Member &m);
};

Member::Member(int argx, int argy)
{
x = argx;
y = argy;
return;
}

Member::Member(const Member &m)
{
x = m.x;
y = m.y;
std::cout << "from copy ctor of Member class : x = " << x << " y = "
<< y << '\n';
return;
}

void Member::operator=(const Member &m)
{
x = m.x;
y = m.y;
std::cout << "from operator= : x = " << x << " y = " << y << '\n';
return;
}

class Test {
public:
Test(const Member &argm1, const Member &argm2, const std::string
&val);
private:
std::string str;
Member member1;
Member member2;
};

Test::Test(const Member &argm1, const Member &argm2, const std::string
&val)
{
str = val;
member1 = argm1;
member2 = argm2;
std::cout << "from one arg ctor : " << str << '\n';
return;
}

int main(void)
{
Member argm1(10, 20);
Member argm2(100, 200);

Test tmp(argm1, argm2, "tmp object");

return 0;
}

If I compile this program with VC++ 2005 Express edition, it gives
compilation error at the line

Test::Test(const Member &argm1, const Member &argm2, const std::string
&val)

saying

'Member' : no appropriate default constructor available

Similar error occurs with g++ also.

If I change this ctor to

Test::Test(const Member &argm1, const Member &argm2, const std::string
&val) : member1(argm1), member2(arg2)
{
str = val;
std::cout << "from one arg ctor : " << str << '\n';
return;
}

then the compilation error goes. Though str is of class type
std::string, str = val is accepted. However the same does not hold for
class type Member for member1 = argm1 and member2 = argm2.
What is the difference ?

Jan 6 '07 #1
2 1544
subramanian napsal:
Consider the following program:

#include <iostream>
#include <string>

class Member {
int x;
int y;
public:
Member(int argx, int argy);
Member(const Member &m);
void operator=(const Member &m);
};

Member::Member(int argx, int argy)
{
x = argx;
y = argy;
return;
}

Member::Member(const Member &m)
{
x = m.x;
y = m.y;
std::cout << "from copy ctor of Member class : x = " << x << " y = "
<< y << '\n';
return;
}

void Member::operator=(const Member &m)
{
x = m.x;
y = m.y;
std::cout << "from operator= : x = " << x << " y = " << y << '\n';
return;
}

class Test {
public:
Test(const Member &argm1, const Member &argm2, const std::string
&val);
private:
std::string str;
Member member1;
Member member2;
};

Test::Test(const Member &argm1, const Member &argm2, const std::string
&val)
{
str = val;
member1 = argm1;
member2 = argm2;
std::cout << "from one arg ctor : " << str << '\n';
return;
}

int main(void)
{
Member argm1(10, 20);
Member argm2(100, 200);

Test tmp(argm1, argm2, "tmp object");

return 0;
}

If I compile this program with VC++ 2005 Express edition, it gives
compilation error at the line

Test::Test(const Member &argm1, const Member &argm2, const std::string
&val)

saying

'Member' : no appropriate default constructor available

Similar error occurs with g++ also.

If I change this ctor to

Test::Test(const Member &argm1, const Member &argm2, const std::string
&val) : member1(argm1), member2(arg2)
{
str = val;
std::cout << "from one arg ctor : " << str << '\n';
return;
}

then the compilation error goes. Though str is of class type
std::string, str = val is accepted. However the same does not hold for
class type Member for member1 = argm1 and member2 = argm2.
What is the difference ?
See
http://groups.google.com/group/comp....cd9695f701fc1f
It is explained there.

In fact you need default constructor for
class X
{
public:
X()
// Here is called default constructor of member
{
member = something;
}

private:
Member member;
};

Jan 6 '07 #2
subramanian wrote:
>
Test::Test(const Member &argm1, const Member &argm2, const std::string
&val) : member1(argm1), member2(arg2)
{
str = val;
std::cout << "from one arg ctor : " << str << '\n';
return;
}

then the compilation error goes. Though str is of class type
std::string, str = val is accepted. However the same does not hold for
class type Member for member1 = argm1 and member2 = argm2.
What is the difference ?
The issue has nothing to do with the assigment (str = val). The issue
is that you don't provide any initializers to the members when they
are constructed, so there needs to be a default constructor.

std::string has a default constructor.
Member doesn't.

If you want the behavior you are looking for, provide a default
construct to member. It could be as simple change as:

Member(int argx = 0, int argy = 0);
Other than the debug prints in the copy constuctor and copy
assignment operators, they do nothing that the implicitly
generated ones wouldn't have done (except for the unconventional
operator= returning void). If this is all they will ever do,
get rid of yours.
Jan 6 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Glenn A. Harlan | last post by:
Why am I receiving the below error when calling - Path.GetTempFileName() The directory name is invalid. Description: An unhandled exception occurred during the execution of the current web...
5
by: Patrick | last post by:
I understand it is built in behaviour that if an ASP.NET's web.config is set to: <customErrors mode="RemoteOnly" /> then I only get a detailed error message on screen when the ASP.NET...
2
by: Brecht Yperman | last post by:
Hi, when calling the XmlSerializer constructor, I get the following error: Top Level Exception Type: System.IO.IOException Message: Unknown Error (-1). Source: mscorlib...
0
by: Marty Cruise | last post by:
I successfully deploy my application to 20 domain users. Only one new user is giving me a problem, although he can access all domain resources. When he clicks the installation link on the...
9
by: subramanian | last post by:
Hello. Consider the following code fragment : enum TestEnum { val1 = 10, val2 = 100, val3 = 1000 }; class Test { public : enum TestEnum { val1 = 1, val2 val3 }; Test(int i = 0, int j = 0,...
5
by: PLS | last post by:
I'm converting some C++ code to VC++ 2005 in native (non-managed) mode. This code doesn't use ATL, but codes the COM mechanisms directly. It has a class which is the equivalent of ATL's...
8
by: 2b|!2b==? | last post by:
typedef struct llist_entry_s llist_entry; /* opaque type */ struct llist_entry_s { llist_entry * next; char * keyword; char * value; llist_entry() :next(0), keyword(0), value(0) {
2
by: f0zzyNUE | last post by:
hi everyone, currently we are testing the performance our application (asp.net based CMS) ... for that reason we wrote a "spider" that starts webrequests for all relevant pages which results in...
0
by: Tifer | last post by:
Hello, I am building my first .Net Application. The first couple of Publish and Installs I did went fine. But after a couple of builds, I get a modal dialogue box error every time upon trying...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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...
0
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
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...

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.