Coders4fun Quiz #3: Will it compile?
Categories: Quiz
Tags:
Will this code compile??? Why?
foo.h
class Foo
{
public:
Foo();
void fooo();
private:
int i;
char c;
short s;
protected:
char b;
char e;
char getFoo();
}
foo.cpp
#include "foo.h"
Foo::Foo()
{
for (int i = 0; ; ) { }
}
void Foo::fooo()
{
while(true)
{
c = 'f';
}
}
char getFoo()
{;
return c + 2;
}
Se sei interessato a questo post, potresti anche provare a leggere:
- No related posts
09 Jan 2008 dzamir
I doubt it will compile…
Something about Foo::getFoo() not implemented or the extra semi-column in the for-loop
I’m sorry, but it’s not the right answer
Sorry again, but the extra semi-column in the for-loop is the right answer. I wrote
Now I corrected the post, but you are anyway the first “winner” 
for (int i = 0; ; ; ) { }
but I wanted to write
for (int i = 0; ; ) { }
So your answer is correct but not the question
Nope.
“char Foo::getFoo()” was not implemented and there for it will not compile.
To cause this program to compile you could change
“char getFoo()” to “char Foo::getFoo()”
It will compile if you don’t implement a function, instead it will not build if you call the undeclared function somewhere in the code, and then your answer is wrong.
I’m thinking no — there should be a semi colon after the closing brace in foo.h
@ Mike:
There is a missing semicolon after the closing brace at the end of the class declaration in foo.h.
Congratulations
I made this error one time and the compiler gave me a lot of error in other c++ files, and I gone crazy to find where was the error
Hmmm… many compilers will allow you to declare a function (Foo::getFoo()) and not define it, so long as its not called. And yes, the semi-colon is an issue, but even still, fix that and it still shouldn’t compile:
char getFoo()
{;
return c + 2;
}
c is not defined at this scope… if it was Foo::getFoo, it would compile.. but there’s no definition in the global scope.
Too lazy to see if anyone else noticed this, but you need a semi-colon at the end of the class.