An initialization with a class involved

T

Tony Young

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
 
X

XHengDF

do not use the default copy constructor(if she could check the uninit
params)
alway give the three important functions maybe well!
V
 
X

XHengDF

do not use the default copy constructor(if she could check the uninit
params)
alway give the three important functions maybe well!
V
 
M

mirpapa

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

n2xssvv g02gfr12930

Tony said:
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
 
P

Peter_Julian

| 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.
 
R

Ravi Nakidi

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
 
T

Tony Young

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 said:
Tony said:
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
 
T

Tomás

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top