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 }
5 odpowiedzi
+ 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.
+ 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
+ 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.
+ 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.
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.