error LNK2001: unresolved external symbol

Started by
34 comments, last by falcon93 12 years, 9 months ago



public:
Sprite(GraphicsEngine graphics, LPCWSTR texture_path, float x, float y, float width, float height);
~Sprite(void);



I'm sending a reference from the GraphicsEngine into the Sprite here, right? But then I have to include the GraphicsEngine header file? Or?

No you are not. You are passing graphics engine by value. In this case you would have to include GraphicsEngine.h in Sprite.h, but this will not solve the problem due to a circular dependency. Instead you should try what I write above, ie. "GraphicsEngine& engine" for the constructor and also storing a reference to the engine: "GraphicsEngine& m_engine;".


Advertisement
Won't that create a new object of the GraphicsEngine? I'm not sure what the & does, but how can the reference know that m_engine actually is graphics_engine?
The & here says it is a reference. A reference is similar to a pointer, so no copy is made. If you pass by value a copy is made.

Won't that create a new object of the GraphicsEngine? I'm not sure what the & does, but how can the reference know that m_engine actually is graphics_engine?


You seem to be confused about what references are:


When you use & in conjunction with a type, you create a reference which does not create a new object. Instead a reference always points to an already existing object. Whereas a value always copies the existing object, thus creating yet another instance:


[source]


GraphicsEngine engine1; //< Now one graphics engine exists

GraphicsEngine engine2 = engine; //< A SECOND engine has been created by copying it from the first

GraphicsEngine& engine1_too = engine; //< this is the SAME as engine

[/source]

The same applies to function & method parameters: If you place a & after the type, then you are passing by reference, if you ommit it (and don't place a *) then you are passing by value.

So, check if this is correct, if it is, I think I've understood it wink.gif



In Main.cpp


GraphicsEngine* graphics = new GraphicsEngine(); // This will create a new object of my GraphicsEngine.

Player* player; // I create my sprite object in the Player class constructor, just to simulate the situation in my game. The Sprite requies variables from GraphicsEngine, therefore:

player = new Player(graphics); // I send a reference. Not a new object or "copy"?






In the Player constructor


Player::Player(GraphicsEngine& graphics) // I can use/write GraphicsEngine + & without having to include the .h file?
{
Sprite sprite = new Sprite(graphics); // Sends a reference to the sprite object
}





In the Sprite constructor
Sprite::Sprite(GraphicsEngine& graphics) // I can use/write GraphicsEngine + & without having to include the .h file?
{
D3DXCreateTextureFromFileEx(graphics.d3ddev, .....);
}



Does this seems correct? Please tell me if anything is wrong, I would really like to learn this smile.gif

Does this seems correct? Please tell me if anything is wrong, I would really like to learn this smile.gif


Almost ;)

Sprite.cpp is required to include GraphicsEngine.h, since you want to call methods on the engine. However you are correct in that you are passing a reference to the engine in both cases, not a copy. You may use the code I posted above (involving that private copy constructor) to forbid copying the engine. If you try to copy it anyway, you will receive a compiler error :)


Sprite::Sprite(GraphicsEngine& graphics, ...)
{
...
}


This won't work if I don't include the GraphicsEngine header. I get errors from IntelliSense saying "GraphicsEngine is undefined", in both Sprite.cpp and Sprite.h sad.gif Any ideas?


*EDIT*

I forgot adding class GraphicsEngine; in my Sprite.h
Now it works. Thanks for the help :)
Ok, I've done as I the examples above, but it still doesn't work proper. I can build the game, but the game freezes/crashes directly after build. I can only use Ctrl + Alt + Delete to get out of the game, and I get no error or help. So I attached my whole project, does anyone want to help me and have a look at it? :(


*EDIT* I'm having trouble on uploading my project... Doesn't the uploader support .rar files?


*EDIT2* Couldn't manage to attach/upload the .rar file. So you can download my project from here instead: http://www.easy-shar...7/AvoidBall.rar. Sorry for external download :(
I attached my whole project, does anyone want to help me and have a look at it? :(

Where is it attached? I can't see it.

When you don't know why a program crash a debugger can be a valuable tool to find the cause.
I'm using Visual Studio 2010 which has a built in debugger, or?

Couldn't manage to attach/upload the file, so I've uploaded it external instead. You can download it here:
http://www.megafileu...idBall-rar.html

[color="#000000"]Sorry for external download :(

This topic is closed to new replies.

Advertisement