![]() |
| ||
| Yet Another Vtable Problem Hi So I've been developing this game for a game design class for most of the semester. While I'm not new to C++ and Object Oriented Design, the theory of it is often a lot easier to comprehend and know than the actual application of it. Needless to say, I've run numerous problems throughout the development of my game, most being surmountable. However, my next goal is to get the code leak free via valgrind on Linux. Unfortunately, I've been developing on windows using Dev-Cpp and the Mingw compiler, and this has spawned some problems. While my code compiles and runs on Windows, when I compile using G++ on Linux, I'm getting the dreaded... Quote:
To try to circumvent this problem, I tried making every function of the base class that was virtual pure-virtual, as to force a definition for each function in each derived class, stub or fully implemented, also in hopes of forcing the vtable for each class into its object file, but much to no avail. So at my wits end, I've come to this forum hopefully to find some answers. Are there any other things that such an error can result from? Or any advice or pointers that can be given? Let me know if I should post my code, as I'm hesitant to because it will be four classes (Base, Derived, More Derived), a lot of code, and I'm not sure if I should omit comments/functions definitions/or what. Thanks in advanced, Yates Monteith |
| ||
| Re: Yet Another Vtable Problem Yes, this case I would post the code. I figure that (a) you have actually googled this. (b) and tried some testing. (c) your using a compiler that give better error messages that VC++. However, this is not a completely uncommon error so I would like to explain what it normally means and if that doesn't fix it then please put some code up. the destructor not adequately defined, normally means that you have a pointer that you cannot find the vtable back to the base. E.g. consider class Base; class A : public Base; class B : public A; No in a separate file you define stuff that uses ptrs/ref or B, and those ptrs get deleted OR those refs get moved out of scope. You don't have sufficient includes in the file, but you have pre-definitions. Then you have an implicit/direct call to a destructor that needs to call A and Base destructor. gcc can't resolve that. So I AM GUESSING!!! that the error is in the lack of an include. However, some code is going to help here so I think some posting is in order. If you have large methods maybe strip them down. |
| ||
| Re: Yet Another Vtable Problem Hi! Thanks for the reply. After reading your post and looking through my code, I think that could possibly be the problem. In my more-derived class, I do have a reference to another class which could be calling the reference out of scope. I guess I'll start off with a description of the hierarchy in question, then post the code with some functions truncated. My basest class is Drawable. It represents objects which have (Read: Private Variables) an x,y coordinate and a x,y velocity. It has a set of non-virtual accessor/mutator functions for speed/coordinates/etc, and a set of pure-virtual functions. I mentioned why these functions are pure virtual in the second post. /* Drawable Class Header */ Duck is a derived class of Drawable. It has a few other private variables, including a vector of type Frame (Another derived of drawable, different hierarchy), a vector of type bullet (A derived of sprite, sprite derived from Drawable), and a couple other primitive data types. It also has all the pure-virtual functions from Drawable defined. Here's the code: #include <vector> Finally, SmartDuck is a derived class of Duck. It only overloads the Update and Draw functions, inheriting everything else, as is. It has a reference to another class (Player, so it can get data access), along with an enumerator, and another primitive in its private section. There are a couple things commented out in the header, those are purely for debugging purposes. #include <iostream> Finally (Sorry, this is getting a little absurd), we have the Player class, immediatly derived from Drawable. #include <vector> There errors I'm getting are: Quote:
Nota Bene: the player object is Drawable*, the ducks object is std::vector<Duck*> void GameManager::loadPlayer() So it seems my errors are coming from Construction and trying to cast pointer objects. The obvious thought is, why not take out the cast in fragDucks? Well, unfortunately, I've introduced a fairly long polymorphic hierarchy (There are still 4 or so classes I haven't posted, but are working fine), and if I don't do the cast, I risk referencing a null pointer, or accessing a function not defined for the class player currently represents. So I'm sorry this got so long, but you've been very helpful so far, and I appreciate any further time you put into looking at my code. Hopefully it doesn't make your eyes bleed too badly. |
| ||
| Re: Yet Another Vtable Problem Sorry I had a quick look at this. The problem is that I cant compile it since there is lots missing and you have a link-time error. Additionally you have missed (I think) a "}" on the duck constructor. Can you post the program as a tar.gz or however you like, but put it as an attachment. (sorry I should have said that in the first message) |
| ||
| Re: Yet Another Vtable Problem 1 Attachment(s) Thanks for the look, perhaps I should've asked about posting it in the first place. The missing brace is most likely a typo in the code that I had to modify before posting. But anyway... Here it is. It's in .zip form because apparently Daniweb doesn't like .tar.gz. I've made a few little adjustments to it to help size and complexity, like disabling a couple in-program debugging features, sound, etc. But a heads up, that caused a number of unused parameter/unused variable warnings, but it shouldn't really have any effect on the heart of the problem. It's C++ / SDL, but the necessary SDL libraries are not included. They're kind of weighty, and one of them (Vanilla SDL) is OS specific. They can easily be downloaded via Dev-Cpps Packaging system, or the Synaptic Package Manager in Linux. Below are links to download them from the official SDL website, where they can be downloaded in compressed form. SDL http://libsdl.org/download-1.2.php SDL_Image http://www.libsdl.org/projects/SDL_image/ SDL_TTF http://www.libsdl.org/projects/SDL_ttf/ SDL_Mixer http://www.libsdl.org/projects/SDL_mixer/ Thanks again for your help, Yates Monteith |
| ||
| Re: Yet Another Vtable Problem Oh I am sorry but I have just LOL big time!!! You forgot to compile and include smartDuck.cpp and player.cpp in you makefile. Well in light of that I would suggest that your next project look at splitting the program into shared libraries built in sub-directories and using some Makefile builder. For this project, just add a player.o and smartDuck.o to your makefile and remember to add it to the link line. p.s. Debugging with the -O2 option is pretty hairy, I would drop that from your makefile [Except production release, but then you drop the -g] Sorry to put you through the trouble of sending a whole project file. |
| ||
| Re: Yet Another Vtable Problem Ugh, talk about a brain fart of massive proportions. A good learning experience though. Maybe someone should add that part to the GCC FAQ, heh. Though could you tell me more about about Makefile builders? Is that a type of application? What are good choices? |
| All times are GMT -4. The time now is 2:38 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC