Object oriented programming

I understand the basics of object oriented programming, but I'm having some issues with applying my knowledge with the cocos2d-x package.

What I'm trying to do is create an object so I can access functions inside another class. While normally this is very simple, when I create an object to my AppDelegate class in my HelloWorld class, while no problems showed in the debugger, when running the program I get a runtime error telling me "Assertion failed! Program ...\cocos2d-2...\ccapplication.cpp Line23 Expression: ! sm_pSharedApplication For more information on how your program can cause an assertion failure, see the Visual C++ Documentation on asserts".

I'm kinda lost as to what to do, as if the problem does lie in the ccapplication.cpp file which is part of the cocos2d-x package, what changes can I make outside of the package to create a working object?
Assertion fail sometimes occur due to corrupt files so please check whether you ccapplication.cpp is working or not.....
An assertion is a test that immediately aborts the application if it fails. It should only be active in a debug build - it's there for developers to immediately be alerted to a mistake they've made.

If a cocos2d-x function/method is triggering an assertion, it's probably because you are somehow not using it correctly. Possibly, you're calling it with invalid arguments, or else there are some other pre-conditions that haven't been met.

Really, you need to consult the documentation for that package, or seek help from someone who's familiar with it.
Unfortunately the support for the c++ community that utilizes cocos2d-x is quite limited, especially for a windows user. Most of the people who use the cocos2d-x package are programming on a mac in objective c, and thus that's where the majority of support is. Documentation for a windows user in c++ is mostly limited, and still in production (incomplete/incorrect tuts).

I know that in objective c, to access the appdelegate you would use something along the lines of YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];, but I have absolutely no idea how that translates into c++.

I've also read somewhere that appdelegate could be a virtual class, and that virtual classes cannot have objects created for them, and thus must use a pointer. However I also do not know how to create a pointer to a function in a different class without having an object to reference to it. And of course, this all assumes that appd is indeed a virtual class.

Hoping someone here could help me figure this stuff out.

Oh and for specific details, here's the line of code that causes the assertion problem :

AppDelegate appd;

I don't even try to utilize the object in code aside from trying to declare it.

Edit : After looking through all the source files, I found that AppDelegate had an object created for it in main(), as AppDelegate app; I took it out of main(), and put it at the top to declare it outside of a function, then used an extern command to declare it in my other class helloworld. This seems to have worked for me, however I still don't understand why I can declare it the main source but not a class source.
Last edited on
when the code breaks can you get inside the callstack and trace it back to the last function it called from your code? If so then make sure whatever you're passing in there is valid.
Actually, the problem is that it doesn't break unless I tell it to retry when the app crashed. Although the debugger still showed what line of code it was running when the app crashed, and it was the line in which I declared the object in the helloworld class.

I'm still trying to wrap my head around why I can create a public object for appdelegate in the main source and externally call it in other classes, but I can't make a private one in main source, and create another private in a different class. Regardless it's probably more efficient by utilizing a global object through external reference, but it doesn't make sense that I can't do it the other way around.
Topic archived. No new replies allowed.