Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problem with constructors - game development  (Read 10648 times)

0 Members and 1 Guest are viewing this topic.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Problem with constructors - game development
« Reply #30 on: September 07, 2015, 04:07:59 pm »
After total of almost 30 posts I get the answer I have been waiting for. :)
In his first post (answer #5), it was correct:

class ClassWithReference
{
    int& x;
public:
    ClassWithReference(int& x_param) : x(x_param) { }
    int& getX() { return x; }
}

Anyway, such mistakes are really trivial to find, even more so when looking at compiler error messages. I can't emphasize enough how important it is to know the basic language syntax. As you see, you can waste tremendous amounts of time for nothing :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

GraphicsWhale

  • Full Member
  • ***
  • Posts: 131
    • View Profile
Re: Problem with constructors - game development
« Reply #31 on: September 08, 2015, 12:18:22 am »
After total of almost 30 posts I get the answer I have been waiting for. :)

You've gotten the answer already, you just didn't seem to have learned anything from it.

If you ever want to make anything significant with C++, or any language, you're going to have to learn how to apply knowledge of the language to your own programs yourself. You are going to run in to scenarios where things are broken in a mess of thousands upon thousands of lines of code and you have absolutely no clue what to do. Even if the problem is really simple, and was just caused by a lack of understanding in the language, you definitely will not be able to post it on a forum and have someone correct it for you.

This question shows a lack of understanding in how objects, a fundamental feature, work in C++. As well as the inability to manage how variables are accessed. This certainly will not be the last problem of yours.


World::World()
: windowManager(windowManager) //This is the initializer list
{
    std::cout << "World constructor" << std::endl;
    //here i initialize tile stuff, no problem, i cut it out.
}
 

It gave me error, but I fixed it by inserting parameter WindowManager& windowManager to constructor.
Now it compiles and doesn't call the constructor twice, thank you.
So it looks like this:

World::World(WindowManager& windowManager)
: windowManager(windowManager)
 

Is that correct way? Or is there something I should do differently?

Yeah, sorry, I must have missed that.

Hapax

  • Hero Member
  • *****
  • Posts: 3366
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Problem with constructors - game development
« Reply #32 on: September 08, 2015, 12:21:12 am »
if you read the code, you see I passed it by value.
I didn't.
I responded to your description of your error and suggested the solution. I was unaware that you...
don't know how to pass it by reference.
Luckily, (this time) the community felt generous and taught you this one C++ thing. Googling is much quicker, I would say  ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

kerp

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Problem with constructors - game development
« Reply #33 on: September 10, 2015, 07:56:19 am »
my 2 cents..

I started completely noob like too.. if it was'nt for a few patient souls telling me things i could not grasp at first i would not have gotten better... so i have a idiom, i just answer the question.

i think hengad's post was just find he did'nt understand why and actually neither did i but i do now! I knew you could change value after passing in a reference but not that constructors were not triggered. And to be honest i need to go through the one good c++ book i have on kindle and read it!!!

But i say A.T.Q, just answer. the. question.

so sorry about ranting i just felt i had to..

a noob in training...

 

anything