Jump to content
  • Advertisement
Sign in to follow this  
Palidorflame

Downcasting problem - C++

This topic is 3754 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi i'm Sean, first time poster. I have a question regarding a downcasting problem in my code. Everything in the code works correctly, except for this partciular part. else if((int)ch == 13 && (typeid(*(currentmenu->options[currentmenu->GetSelectedIndex()])) == typeid(MenuOption))) { menuStack.Push(currentmenu); currentmenu = GetFullMenu(currentmenu->options[currentmenu->GetSelectedIndex()]. ????????? system("cls"); currentmenu->Show(); cout << "You pressed the enter key.\n"; } The place i have the problem is where the ???'s are. What I am doing is using a function to read data in from a file, and it works fine (fully tested). Since options is a dynamic array of Option's, and MenuOption's (Option's child), i want to access the method of the object at the given index. But when i try to type the . or -> operator, i can't access that object's methods. I'm really stuck with this, and i was thinking it might be a simple dynamic_cast issue, but i can't seem to get it to work. Please help! Note: currentmenu is of class type FullMenu, which contains a dynamicarray called options that holds both Option's (menu selections that haven't been implemented yet) and MenuOption's (menu selections that can be used to open subsequent menus). Here's the class for FullMenu. class FullMenu { public: FullMenu(const char* title = "Unnamed", int SelectedIndex = 0); ~FullMenu(); void SetSelectedIndex(int num); void Show(); char* GetTitle() const; int GetSelectedIndex() const; DynArray<Option*> options; private: int m_nSelectedIndex; char* m_szTitle; };

Share this post


Link to post
Share on other sites
Advertisement
I just tried that and still couldn't access my methods. Any other ideas? :-( In case you didn't catch it also, the DynArray is filled with Option pointers, if that has any relevance.

Share this post


Link to post
Share on other sites
Ok i changed it to

currentmenu = GetFullMenu(dynamic_cast<MenuOption>(currentmenu->options[currentmenu->GetSelectedIndex()]).GetFileName());

So that gets that file for the new menu, generates the menu, and returns it to the currentmenu.

2 errors:

left of ',GetFileName' must have class/struct/union

&

'MenuOption': invalid target type for dynamic_cast

I've been at this for hours...

Share this post


Link to post
Share on other sites

currentmenu = GetFullMenu(dynamic_cast<MenuOption*>(currentmenu->options[currentmenu->GetSelectedIndex()])->GetFileName());



Try this version. Now the cast should work.

Share this post


Link to post
Share on other sites
Ok, intellisense is still not picking it up, but now i debugged it down to a point after that line, and it is successfully returning the filename into the function call, so here is my file input function where the new crash is happening.

FullMenu* GetFullMenu(const char* filename)
{
char temp[NUM_SIZE];
string tempstring, tempstring2;
int numoptions = 0, numvalue = 0, iter = 0, quotecount = 0;
FILE * filey = fopen(filename, "r");
fgets(temp, NUM_SIZE, filey);

It's crashing on the

fgets(temp, NUM_SIZE, filey);

When i hover over filename, it has the string, with quotes, for the filename. But now filey is undefined by the time it does fgets. It has no problem the first time i call this function. And i do close the previous file at the end of the function.

Share this post


Link to post
Share on other sites
Ok i added the ferror check as follows:

FullMenu* GetFullMenu(const char* filename)
{
char temp[NUM_SIZE];
string tempstring, tempstring2;
int numoptions = 0, numvalue = 0, iter = 0, quotecount = 0;
FILE * filey = fopen(filename, "r");
if(ferror(filey) != 0)
{
cout << "The file did not open.\n";
}
fgets(temp, NUM_SIZE, filey);

However, not it crashed at the

if(ferror(filey) != 0)

dragging ferror(filey) to the watch window shows

Error: symbol "ferror" not found

but that would make sense, since filey was undefined after the

FILE * filey = fopen(filename, "r");

I'm still confused.... lol

End result: filey is still undefined, and i can't figure out why. It should work...


Oh, the reason i'm using this instead of fstream is because at Full Sail, we are learning code optimization, and including fstream would add a lot of overhead stuff that wouldn't be needed or wanted in a large scale game. And we're building code that we'll use in projects to come, so i wanted to make the most speed-based menu system i could.

Share this post


Link to post
Share on other sites
So you are trying to solve a problem you don't have with code you don't understand, interesting. A quick look at TFM(why doesn't anyone RTFM) reveals that fopen returns null when there is an error when opening the file, and as it is a C function you should include errno.h and use its functions to get a reasonable error message when it fails to open a file.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!