+ 4
We have not an error because of parentheses. Why??
#include <iostream> class A { public: A() { std::cout << "default!" << std::endl; } A(int i=0) { std::cout << "integer!" << std::endl; } }; int main() { A a(); return 0; }
11 Respuestas
+ 6
What is the error that you are facing?
+ 3
First of all, the constructors are ambiguous.
A() takes 0 arguments,
A(int=0) takes 0 or 1 arguments.
So if you try to do A a; the compiler doesn't know which one to call.
Secondly A a(); is a function declaration, not an object construction, remove the ().
+ 3
Yea, I already thought this question comes up way too frequently so it must be something popular somewhere, just didn't know where it came from.
Anyway,
you are not calling the constructor, just telling the compiler that a function exists somewhere that returns an A object and is called a which takes no arguments. That is what a function declaration is.
However there is no code anywhere that instruct the compiler to actually construct the A object, so the compiler doesn't even need to look at the constructors of A, hence there is no error.
+ 3
The only reason I can come up with right now is if you only want certain functions to access other functions, but disallow others, why, I dunno.
void f1()
{
A a();
a();
}
void f2()
{
a(); // Error here
}
A a(){ return {}; }
While if A a(); was declared globally it would all compile fine.
But that is still error prone because the function could still be defined above the other functions. So yea :)
+ 2
I think he means when typing
A a;
we get an error because it can't determine which constructor to choose.
However using
A a();
gives no output. It doesn't show any error message but doesn't seem to work as expected.
+ 2
@Matthias
There is no error until you actually try to use it.
The compiler is only told that it exists somewhere, it does not care whether it actually exists. That is the linker's job and the linker only cares if you try to use the function.
( undefined reference errors are from the linker )
Also your function would crash because it doesn't return an A object.
+ 1
That's the point. Compiler can not choose the the suitable constructor in both cases (A a, and A a()).But it gives an error message only without parentheses. Why?
+ 1
Dennis first of all, it is not my code, it's some exercise from SL, which I found confusing. Do you mean,that if I call the constructor AS FUNCTION to create an object, there ist always no error from compiler, even if it's not determine which constructor it should choose?
+ 1
Shouldn't it output an error then, that this function could not be found?
Even if there is a function
A a(){ std::cout<< "hey"; A b(2); return b;}
It doesn't show anything, only if just use a(); of course.
+ 1
Oh yeah. In the code playground I returned an object, just forgot it to write it in my post 🙉
Ok, so
int b();
Also shows no error since this is just a function declaration (without body, i.e. no definition). I wonder why this even works in main function 🤔 Didn't know one could declare a function inside main 😅
But
int i = b();
for example gives an error, since b is used but cannot be found. Ok, I think I slowly understand...
Weird behavior though 😶
+ 1
Yes, I just played a bit with the different scopes in playground.
I have never seen decleration inside functions though 😅 But good to know this feature exists^^
//void b(); // #1
void a(){
void b(); // #2
b();
}
int main() {
//int b();
a();
b();
return 0;
}
void b(){
cout<<"b";
}
Deleting #1 and #2 gives error, since b is not declared anywhere.
Having only #1 outputs bb.
Having only #2 gives error for b(); in main, because not declared in this scope.
Having both looks ugly and maybe that b inside a is actually a copy or so (have to check), but it also works and outputs bb.
Cool, learned something new today :D