compiling problems with g++

Started by
4 comments, last by Fruny 18 years, 7 months ago
Hello, I use the compiler Forte C++ 6.update 2 and when I'm trying to compile something with g++ I receive the errors: include/abl/List.h:151: warning: `class List<void *>' has virtual functions but non-virtual destructor include/MyObject.h:151: warning: `class MyObject' has virtual functions but non-virtual destructor etc.. If I compile the same programm but with CC it compiles succesfull. Do you think it's a compiler problem? What I want to do is to compile my programm as a dynamic library (as a static library I succeded). Thank you for your time.
Advertisement
Those are not errors; just warnings. And it's not a compiler problem, you are doing something that is likely to end up causing problems. If a class has virtual functions then you should give it a virtual destructor.
Just change ~MyObject(); into virtual ~MyObject(); and you're fine.

Secondly, I couldn't help notice that you're compiling a templated class, called List<void *>.
Is there a reason for using a custom-made class, as opposed to using std::list ?
what happens when you provide a virtual distructor for the classes that give you an error like this? (just write virtual infront of the destructor)
“Always programm as if the person who will be maintaining your program is a violent psychopath that knows where you live”
The error disappears for one. Also derived classes are allowed to override the destructor, allowing them to perform their own destruction tasks (I think I got that right).

BTW, for the most part all compiler errors are generated by the programmer, and not by the compiler. The reason you're getting the warnings is because Visual C++ is a more lenient compiler - a lot of code that compiles on VC will not compile with G++ because G++ is very strict in terms of code correctness. This is very good for your programming habits, of course, but it can take some getting used to ;).

Cheers!
- fyhuang [ site ]
If a class has virtual functions, that means that you plan on using it polymorphically. I.e. keep a pointer or reference to the base class around, while the object really is of a derived type. Base* ptr = new Derived;.

The question is, what will happen when you try to destroy that object? delete ptr;. If the destructor isn't virtual, you will not get polymorphic behaviour for that operation, and you will end up calling the wrong destructor, which will definitely cause problems (officially, it has undefined behaviour, meaning "we take no responsibility for what happens if you do it".)

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement