|
Interesting GCC 'bug' |
Edgar Reynaldo
Major Reynaldo
May 2007
|
I banged my head against the wall for a while with this one. Imagine you have a header like this : #include "DeclaredBase.hpp" #include "Forward.hpp" class Derived : public DeclaredBase { Forward* f2; }; And every time you include it the compiler chokes on the Forward* line, saying : main.cpp:16:4: error: 'Forward' does not name a type I couldn't figure out what was going on, so I thought, well there must be something wrong with the Forward class. This is all it is : class Forward { int blah; }; Okay well obviously there's nothing wrong with that. Let's check out the base class : class Forward; class DeclaredBase { Forward* pforward; public : Forward* Forward() {return pforward;} }; Well the compiler didn't complain about DeclaredBase at all, but if you look closely, the member function declared with the name Forward compiles just fine, but it hides the class with the name Forward. Even if the Forward class is fully declared, the member function hides the class. Here's the full example : 1
2
3class Forward {
4 float fwd;
5};
6
7
8
9class DeclaredBase {
10 Forward* pforward;
11public :
12 Forward* Forward() {return pforward;}
13};
14
15
16
17class Derived : public DeclaredBase {
18 Forward* f2; 19};
20
21
22
23int main(int argc , char** argv) {
24
25 Derived d;
26 (void)d;
27
28 return 0;
29}
And the compiler message stays the same : main.cpp:18:4: error: 'Forward' does not name a type Forward* f2; ^~~~~~~ I think the compiler should know better than that and complain, but it gives you a false error message, because the class Forward was clearly defined. EDIT Severity Code Description Project File Line Suppression State Error (active) E0757 function "DeclaredBase::Forward" is not a type name Project1 c:\ctwoplus\progcode\test\temp\main.cpp 18 Error C2143 syntax error: missing ';' before '*' Project1 c:\ctwoplus\progcode\test\temp\main.cpp 18 Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int Project1 c:\ctwoplus\progcode\test\temp\main.cpp 18 Error C2238 unexpected token(s) preceding ';' Project1 c:\ctwoplus\progcode\test\temp\main.cpp 18 Anyone else have any interesting compile bugs they'd like to share? My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Peter Hull
Member #1,136
March 2001
|
FWIW clang has a more helpful message, including how to fix it: c++ eduardo.cc -o eduardo eduardo.cc:11:4: error: must use 'class' tag to refer to type 'Forward' in this scope Forward* f2; ^ class eduardo.cc:6:22: note: class 'Forward' is hidden by a non-type declaration of 'Forward' here public: Forward * Forward() { ^ 1 error generated. make: *** [eduardo] Error 1
|
Chris Katko
Member #1,881
January 2002
|
Clang really is a higher tier of compiler than the rest. GCC optimizes at all passes which actually throws away metadata that then cannot be used for error messages at later passes. It used to be the "common" thing to do (hence MSVC as well). Clang/LLVM is a smarter design after decades of learned lessons and spilled blood. -----sig: |
Edgar Reynaldo
Major Reynaldo
May 2007
|
Too bad CMake doesn't support Clang, that really sucks. Anyway, creditz! My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Kitty Cat
Member #2,815
October 2002
|
Edgar Reynaldo said: Too bad CMake doesn't support Clang, that really sucks. Clang works just fine with CMake, I use it often. If you're on Linux and Clang isn't your default compiler, you can do: CC=clang CXX=clang++ cmake [other cmake options here] with a fresh build directory. CMake and the generated makefile will remember the compiler, so you only need to set CC and CXX again when starting a fresh build directory. -- |
Edgar Reynaldo
Major Reynaldo
May 2007
|
What 'Makefile Generator' am I supposed to use with CMake? My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Kitty Cat
Member #2,815
October 2002
|
Any you can use on your system, I'd think. I've used Unix Makefiles and Ninja. -- |
Edgar Reynaldo
Major Reynaldo
May 2007
|
What I'm getting at, are gcc's compile options compatible with clang? My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Peter Hull
Member #1,136
March 2001
|
Edgar Reynaldo said: What I'm getting at, are gcc's compile options compatible with clang? Should be; the clang compiler driver is described as a 'drop-in substitute for gcc` Also on Windows there is clang-cl which is the equivalent drop-in replacement for Microsoft's compiler (cl)
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
w.o.w. It's like someone made an Easy button : {"name":"iStock_29117266_XXLARGE.jpg","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/2\/a\/2af48fbc3417383d60c833763b30c9e5.jpg","w":1200,"h":1120,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/2\/a\/2af48fbc3417383d60c833763b30c9e5"} My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
|