-1

I am trying to print out xx, yy values of an object which gets those values from a generator class where I assign them. For example:

Object.h

class Object {
public:
  // Define default constructors as this is an abstract class
  Object() = default;
  Object(const Object&) = default;
  Object(Object&&) = default;
  virtual ~Object() {};

  // Object properties
  glm::vec3 position{0,0,0};
  glm::vec3 rotation{0,0,0};
  /* I tried to define those variables here instead 
  of defining in cube.h but on print, values are never changed. */
  //int xx=-1;
  //int yy=-1;

protected:
  void generateModelMatrix();
};

Cube.h:

#include "object.h"

class Cube final : public Object{

private:
   //some code

public:
   int xx=-1;
   int yy=-1;

   Cube();

private:
};

Generator.cpp

#include "cube.h"
bool Generator::update() {
   for (int x = 0; x < 5; ++x) {
       for (int y = 0; y < 5; ++y) {
          auto obj = make_unique<Cube>();
          obj->position.x += 2.1f * (float) x - 4.0f; //this works fine
          obj->xx = x;   //In this case, I get debugger error for xx: -var-create: unable to create variable object.
          obj->yy = y;   //same error here
       }
    if(x==4) return false;
   }
return true;
}

Cube.cpp

#include "cube.h"

Cube::Cube(){
   printf("x: %d, y: %d\n", xx, yy);
}

And in class Cube I tried to print out xx and yy values and I get -1 for every object, obviously it is not assigning those values at all. What did I do wrong?

Ady96
  • 686
  • 4
  • 12
  • 35

1 Answers1

3

The constructor is called when the object is created. Which for the Cube objects happens with the make_unique call.

You set the values after the objects are created, long (relatively speaking) after the Cube constructor have returned.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • So but since I set value for `obj->xx` how can I assign it before? – Ady96 Nov 17 '17 at 17:41
  • 1
    @Ady96 Pass them as argument to the constructor. And use a *constructor initializer list* for the member variables. All things [a good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) should have told you how to do. – Some programmer dude Nov 17 '17 at 17:43
  • Okay. But I don't understand how can `position.x` get value and `xx` can not? – Ady96 Nov 17 '17 at 17:45
  • @Ady96 `position.x` doesn't have a value yet, either. You just don't print it out in the constructor. – melpomene Nov 17 '17 at 17:47
  • @Someprogrammerdude thank you, I really did not realize that the value is actually assigned but "visible" after the cube is created. – Ady96 Nov 17 '17 at 17:51
  • @melpomene and yet, you won't remove your vote down, because you still think its a bad question, even though, some people can answer it without hesitation, right? – Ady96 Nov 17 '17 at 21:07
  • @Ady96 Hey, I upvoted this answer (because it is what helped me understand what your question was even about it the first place). Your question *is* still bad: It shows no research effort and it is unclear. Both of these issues could have been avoided if you had just posted a [mcve], like you were told hours ago (by 3 different people IIRC). On this version of your question I even wrote example code for you (link to minimal version is in my comment above). – melpomene Nov 17 '17 at 22:58
  • @melpomene your link is not minimal, not complete and not verifiable example. So you are no help at all. Waste of time and database storage for your comments. – Ady96 Nov 17 '17 at 23:01
  • @Ady96 How is it not minimal, complete, and verifiable? It's a fairly short piece of code (single file, 15 lines) that compiles and outputs `-1` for `xx`. – melpomene Nov 17 '17 at 23:03