Sign in to follow this  
flammable

2 c++ problems

Recommended Posts

Hey, I'm having two problems while trying to make a game (I'm using Visual Studio 2010 Express)

[b]1) Trouble with renaming a namespace:[/b]
What I'm trying to do is put all classes in an namespace of an library I'm using into my own namespace, so I don't have to work with different namespaces.

what i'm doing is:
[code]namespace Engine
{
using namespace sf;
}

// namespace Engine = sf; doesn't work because the namespace already exists[/code]
But If I do this I get all kinds of errors about types that are not convertable when using templates (smart pointers). Like:
[code]
error C2440: 'initializing' : cannot convert from 'boost::shared_ptr<T>' to 'boost::shared_ptr<T>'

with
[
T=sf::RenderWindow
]
and
[
T=Engine::RenderWindow
][/code]I do understand why this error occurs but is there a work around or do I have to use 2 namespaces?

[b]2) A bad combination of inheritance, polymorphism and void pointers.[/b]

I have a listener class with callback functions which other classes inherit from, using virtual functions the overridden function is called, quite standard. But the catch is:
The physics engine I'm using stores userdata as void* pointers, what I try to do is store the listener in the userdata (as void*) and then cast it to the listener class pointer and call the callback function but that where it goes wrong.

This is the simplified problem:
[code]
class Listener
{
public:
virtual void onEvent() = 0;
};

class MyListener : public Listener
{
public:
virtual void onEvent() {};
};

int main()
{
MyListener* listener = new MyListener();
PhysicsBody.userData = static_cast<void*>(listener);

do things
}

void onEvent(PhysicsBody body)
{
Listener* listener = static_cast<Listener*>(body.userData);
listener->onEvent(); // Runtime error about memory violation;

// dynamic_cast<Listener*>(body.userData) // gives a compilation error
}
[/code]
I'm guessing the error is caused because the casting to void* discards the runtime information about the pointer and then when you cast it back try to use it again. But the question is what to do about it? I'm quite clueless and there don't seem to be any solution without completely redesign my programming.

Thanks in advance.

Share this post


Link to post
Share on other sites
I would try something like this for your second problem:


[code]
int main()
{
Listener* listener = new MyListener();
PhysicsBody.userData = static_cast<void*>(listener);

do things
}

void onEvent(PhysicsBody body)
{
Listener* listener = static_cast<Listener*>(body.userData);
listener->onEvent();
}
[/code]
[color="#1C2837"][size="2"]Otherwise the void* will contain the address to a MyListener class, and not the Listener base-class.[/size][/color][color="#1C2837"][size="2"](I might be wrong)[/size][/color]

Share this post


Link to post
Share on other sites
[quote name='flammable' timestamp='1312918088' post='4846834']
[code]namespace Engine
{
using namespace sf;
}
[/code]
[/quote]

What this does is put all the names that are in sf in the Engine namespace if you use this in a header that is included everywhere you get a lot of double definitions for types. These definitions however are still distinctly different for the compiler because of the name space on some of them and thats why they don't cause a compile error on duplicates. Because you are using your types without namespaces at that point the compiler isn't sure which type you actually meant to use.
[code]
namespace sf
{
class FooBar;
}


namespace Engine
{
using namespace sf;
class FooBar;

FooBar instance(); //Compiler gets confused here
}
[/code]


If you try to use FooBar somewhere where the compiler knows about both namespaces it will fail as it will find a definition that matches the symbol in two places.




This is why you should never use "using namespace std" and the like in a header file, in a cpp file this won't cause and issue.

Share this post


Link to post
Share on other sites
Thanks for the quick replies :D.
[member='panic'] your solution worked and my second problem is now solved :)
[member='NightCreature83'] your post was a big ohhhh... "I should have known" moment, I understand what the problem is know, guess I keep things as they are now.

Thanks again.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this