473,406 Members | 2,378 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,406 software developers and data experts.

An initialization with a class involved

Hi,

The following code will have a compile error unless I remove "int *p"
from class D (the error message is attached below). But I do need the
*p. Someone suggested to change the initialization code to
"S s[] = {D(d1, 3), D(d2, 4)};" assuming the constructor is provided.
Is this the only way to resolve this problem? I prefer to modify class
D and keep the initialization code intact. Would it be possible? Any
help is much appreciated. Thanks. Tony

class D
{
int *p;
};

struct S
{
D d;
int x;
};

int main(int argc, char* argv[])
{
D d1, d2;
...
S s[] = {{d1, 3}, {d2, 4}};
return 1;
}

Ps.
The error message:
cannot convert from 'class D' to 'struct S'
No constructor could take the source type, or constructor overload
resolution was ambiguous
Mar 16 '06 #1
8 3102
do not use the default copy constructor(if she could check the uninit
params)
alway give the three important functions maybe well!
V

Mar 16 '06 #2
do not use the default copy constructor(if she could check the uninit
params)
alway give the three important functions maybe well!
V

Mar 16 '06 #3
Tony Young wrote:
Hi,

The following code will have a compile error unless I remove "int *p"
from class D (the error message is attached below). But I do need the
*p. Someone suggested to change the initialization code to
"S s[] = {D(d1, 3), D(d2, 4)};" assuming the constructor is provided. Is
this the only way to resolve this problem? I prefer to modify class D
and keep the initialization code intact. Would it be possible? Any
help is much appreciated. Thanks. Tony

class D
{
int *p;
};

struct S
{
D d;
int x;
};

int main(int argc, char* argv[])
{
D d1, d2;
...
S s[] = {{d1, 3}, {d2, 4}};
return 1;
}

Ps.
The error message:
cannot convert from 'class D' to 'struct S'
No constructor could take the source type, or constructor overload
resolution was ambiguous


Do you mean the changed code has the error? Also "int *p" is a
private member of D and hence currently inaccessible. At present
the error message appears to apply to the changed code and not
the original, (in which case it's not surprising).
Hence please clarify both the original and changed code with
the relevant errors and the lines for which they occur. Then
the problem may become clearer.

JB
Mar 16 '06 #4
Well, I have no error message with that code in my linux machine.
I used g++ 3.3.3 (SuSE Linux).
What's wrong?

Mar 16 '06 #5

"Tony Young" <jd*******@yahoo.com> wrote in message
news:Dg*******************@newssvr14.news.prodigy. com...
| Hi,
|
| The following code will have a compile error unless I remove "int *p"
| from class D (the error message is attached below). But I do need the
| *p. Someone suggested to change the initialization code to
| "S s[] = {D(d1, 3), D(d2, 4)};" assuming the constructor is provided.
| Is this the only way to resolve this problem? I prefer to modify class
| D and keep the initialization code intact. Would it be possible? Any
| help is much appreciated. Thanks. Tony
|
| class D
| {
| int *p;
| };
|
| struct S
| {
| D d;
| int x;
| };
|
| int main(int argc, char* argv[])
| {
| D d1, d2;
| ...
| S s[] = {{d1, 3}, {d2, 4}};
| return 1;
| }
|
| Ps.
| The error message:
| cannot convert from 'class D' to 'struct S'
| No constructor could take the source type, or constructor overload
| resolution was ambiguous

C++ coders are notoriously terrible at guessing. What are you trying to
achieve? Is the private pointer in class D supposed to point the integer in
struct S or is it supposed to point to something else?

While POD types present a certain level of simplicity, they more often than
not complicate the code with unexpected bugs. In the case above, all the
pointers are guarenteed to be invalid. Which is a big no-no in C++. A
properly designed class can save weeks worth of debugging.

Consider code that guarentees valid pointers or don't use pointers at all.
If you think there is no way to achieve this, think again.

You can design your classes to guarentee that their pointers are well
defined with a default ctor, copy ctor and operator=. Its always preferable
to have a pointer set to null (0) rather than dangling.

Additionally, in the case you needed a pointer to that integer, the trick is
to declare the integer before the pointer in struct S. Members are always
initialized in the order they are declared and thats where the init list
becomes a very powerful feature.

Mar 16 '06 #6
H Tony,

Nothing wrong U'r program here. But, some of the compilers throughs
error because of two different classes. In this case U have to
explicitly provide type cast or operator function(which is provided by
C++. I think this already u know). This program doen not pop up any
eeror in Vc++, linux and unix compilers. If, u got any problem again,
plz send entire code, so that I'll provide good solution. But, in above
nothing is wrong.

Regards,
Ravi Nakidi

Mar 16 '06 #7
Hi,

If I include *p in the class D, then the compiler generates the error
message against the initialization line, namely "S s[] = {{d1, 3}, {d2,
4}};". I think whether *p is private or not shouldn't affect this
because I haven't accessed *p yet in the code.

Tony
n2xssvv g02gfr12930 wrote:
Tony Young wrote:
Hi,

The following code will have a compile error unless I remove "int *p"
from class D (the error message is attached below). But I do need the
*p. Someone suggested to change the initialization code to
"S s[] = {D(d1, 3), D(d2, 4)};" assuming the constructor is provided.
Is this the only way to resolve this problem? I prefer to modify
class D and keep the initialization code intact. Would it be
possible? Any help is much appreciated. Thanks. Tony

class D
{
int *p;
};

struct S
{
D d;
int x;
};

int main(int argc, char* argv[])
{
D d1, d2;
...
S s[] = {{d1, 3}, {d2, 4}}; return 1;
}

Ps.
The error message:
cannot convert from 'class D' to 'struct S'
No constructor could take the source type, or constructor overload
resolution was ambiguous

Do you mean the changed code has the error? Also "int *p" is a
private member of D and hence currently inaccessible. At present
the error message appears to apply to the changed code and not
the original, (in which case it's not surprising).
Hence please clarify both the original and changed code with
the relevant errors and the lines for which they occur. Then
the problem may become clearer.

JB

Mar 16 '06 #8
Tony Young posted:
Hi,

The following code will have a compile error unless I remove "int *p"
from class D (the error message is attached below). But I do need the
*p. Someone suggested to change the initialization code to
"S s[] = {D(d1, 3), D(d2, 4)};" assuming the constructor is provided.
Is this the only way to resolve this problem? I prefer to modify class
D and keep the initialization code intact. Would it be possible? Any
help is much appreciated. Thanks. Tony

class D
{
int *p;
};

struct S
{
D d;
int x;
};

int main(int argc, char* argv[])
{
D d1, d2;
...
S s[] = {{d1, 3}, {d2, 4}};
return 1;
}

Ps.
The error message:
cannot convert from 'class D' to 'struct S'
No constructor could take the source type, or constructor overload
resolution was ambiguous

Compiled with no errors and no warnings for me.

-Tomás
Mar 16 '06 #9

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

Similar topics

1
by: Qin Chen | last post by:
I will present very long code, hope someone will read it all, and teach me something like tom_usenet. This question comes to me when i read <<Think in C++>> 2nd, chapter 10 , name control,...
21
by: JKop | last post by:
Is this them all?: class Dummy { public: Dummy() {} Dummy(const Dummy& original) { }
17
by: Thomas Lorenz | last post by:
Hello, I'm a little confused about object initialization. According to Stroustrup (The C++ Programming Language, Special Edition, Section 10.2.3) constructor arguments should be supplied in one...
2
by: Matthias Kaeppler | last post by:
Hi, say I have an arbitrary class Bar: 1 Bar a; 2 Bar b(a); 3 Bar c = a; In line 3, is the default ctor called for c _first_ and _then_ the assignment operator, or is c never default...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
2
by: timlyee | last post by:
int *p = new int; auto_ptr<intap1 = p; //will fail on 3 1 auto_ptr<intap1(p); //ok 2 *ap1 = 12; // 3 the first situation has called :...
2
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; class Base { public: Base(int x = 0);
11
by: subramanian100in | last post by:
Suppose we have a class named Test. Test obj; // assuming default ctor is available Test direct_init(obj); // direct initialization happens here Test copy_init = obj; // copy initialization...
1
by: Old Wolf | last post by:
In 12.6.1 of n2521, it says: "When an aggregate (whether class or array) contains members of class type and is initialized by a brace- enclosed initializer list, each such member is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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.