Simple file with polymorphism compilation problem [closed]
I have problem with compile some very simple code and I don't know what is wrong with it :/ Take a look:
class A{
public:
virtual void func() = 0;
};
class B : public A
{
public:
virtual void func() {};
};
int main()
{
A* obj = new B();
return 0;
}
This is the message I get from g++:
Info: resolving vtable for _cxxabiv1::__si_class_type_info by
linking to __imp __ZTVN10__cxxabiv120__si_class_type_infoE (auto-import)
Info: resolving vtable for __cxxabiv1::__class_type_info by
linking to __imp___Z TVN10__cxxabiv117__class_type_infoE (auto-import)
k:/mingw/bin/../lib/gcc/mingw32/4.5.0/../../../../mingw32/bin/ld.exe:
warning: auto-importing has been activated without
--enable-auto-import specified on the c ommand line. This should work
unless it involves constant data structures referencing symbols from
开发者_如何学C auto-imported DLLs
You don't have any problems with compiling, you have problems (more precisely, just warnings) with linking. See:
d:\alqualos\pr\testpq>g++ -Wall -c main.cpp
main.cpp: In function 'int main()':
main.cpp:14:8: warning: unused variable 'obj'
d:\alqualos\pr\testpq>g++ -Wall main.o
Info: resolving vtable for __cxxabiv1::__si_class_type_info by linking to __imp_
__ZTVN10__cxxabiv120__si_class_type_infoE (auto-import)
Info: resolving vtable for __cxxabiv1::__class_type_info by linking to __imp___Z
TVN10__cxxabiv117__class_type_infoE (auto-import)
d:/mingw/bin/../lib/gcc/mingw32/4.5.0/../../../../mingw32/bin/ld.exe: warning: a
uto-importing has been activated without --enable-auto-import specified on the c
ommand line.
This should work unless it involves constant data structures referencing symbols
from auto-imported DLLs.
This is a widely encountered "feature" of MinGW. It happens if your program needs anything from the standard C++ library, even simply std::cout. In this case it has something to do with vtables. To get rid of it:
d:\alqualos\pr\testpq>g++ -Wall -Wl,--enable-auto-import main.o
I have no idea what "should work unless it involves constant data structures referencing symbols from auto-imported DLLs" means. I have tried googling it but haven't found anything useful. If anyone figures out what it really means and when it could be dangerous, please post an answer here.
try A * obj = new B;
, however in the future please post the error message as well.
The code that your post here meets all requirements of C++ standart. So problem is in compiler, I've tried to compile it in VS(Windows) and g++(Linux), and in both of them compilation successed.
I test this on another computer and it's working, so I think problem is with compilator on one computer.
Just a guess, but have you tried to add virtual destructor to base class? Maybe this will help.
精彩评论