0

[solved]Why the cpp code no output?

This is a Quiz class A { public : A (){ cout <<" Class A !";}//Why not output this A ( int a=0){ cout <<" A ";} //is the definition useless? }; int main (){ A a(); //No output }

18th Aug 2018, 12:48 AM
Xhy
Xhy - avatar
5 Respostas
+ 1
Is this what you are trying to get it to do? I can explain if so. I have a tough time helping if I dont know what result you want. Yours has no output because you are confusing the compiler as to what you want it to do with a(). https://code.sololearn.com/cy47fX8lhdZw/?ref=app Roman Khristoforov is explaining what is happening.
18th Aug 2018, 1:50 AM
Mike Utty
Mike Utty - avatar
+ 1
there are two explicit default constructors. If you pass some number into constructor you will see "A". Otherwise, there is ambiguos constructor calling. Because the second constructor has default value of its parameter. Compiler doesn't know which constructor you call. Usually this situation terminate compilation. I think this is a feature of the implementation of sololearn
18th Aug 2018, 12:59 AM
Roman Khristoforov
Roman Khristoforov - avatar
+ 1
Xhy In main, when you call A a();, instead of constructing an object of type A, this statement is evaluated as a prototype for a function that accept no arguments and returns an object of type A. Thus, nothing happens. So, to call a default constructor, omit the parenthesis : Eg = A a; Note that this returns an ambiguity error due to the reason mentioned by Roman Khristoforov above.
18th Aug 2018, 3:21 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
I got it.Thanks for all of you. Cause a() isn't defined.Then nothing output. Just change a() to b(), the result is same.
18th Aug 2018, 5:53 AM
Xhy
Xhy - avatar
0
Mike Utty I know your code can be compiled and output "Class A".But my question is Why, not How to implement right.So please tell me why my code output nothing, thx.
18th Aug 2018, 1:23 AM
Xhy
Xhy - avatar