0

Okay so i have a pointer vector of type 'Entity', and what i want to do is, when a new Entity type class gets constructed it gets pushed back to the vector of Entities, in C# this would be possible by passing 'this' as a parameter, in C++ it gives no error, but when i test it, the vector doesn't point to the newly contructed Entity !

Here is some code:

'Public.h' and 'Public.cpp' handle the public variables and functions, here is the pointer vector and pointer variable

vector <Entity*> AllEntities;
Entity* lastEntity;

And here is the constructor of the class 'Entity'

'Entity.cpp':

#include "Public.h"
#include "Entity.h"

// constructor
    Entity::Entity(string name, string tag)
    {
        ID = GetCounter();
        this->Name = UniqueName(name);
        this->Tag = UniqueTag(tag);
        AllEntities.push_back(this); // it doesn't give any errors
        lastEntity = this; // because i thought it was a problem with the vector i tried the same with a variable, but it doesn't work too
    }

// Function that prints the name, tag, and id.

void Entity::PrintAll(){
    cout << "NAME: \"" << Entity::Name << "\" TAG: \"" << Entity::Tag << "\" ID: \"" << Entity::ID << "\"" << endl;
}

// other code

'Entity.h' doesn't do much, it only handles the declaration(or definition, not sure how its called) of variables and functions like Name, Tag, and ID !

and here is my 'Main.cpp':

#include "SFML\Graphics.hpp"
#include "SFML\System.hpp"
#include "Public.h"
#include "DisplayText.h"
#include "SaySomething.h"
// note that i'm including 'Entity.h' through 'Public.h' with Include Guards so i won't get any linker errors !

int main()
{
// sfml stuff !
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");

    Entity ent1 = Entity("ent1");

    ent1.PrintAll();

    AllEntities.at(0)->PrintAll();

    lastEntity->PrintAll();

// other code

Entity ent1 = Entity("ent1");

here i create a new entity, with the name "ent1", and it automatically gives it the tag "Entity0" and the ID "0" because its the first entity !

ent1.PrintAll();

This works correctly, it prints out the name("ent1"), tag and ID of this Entity !

AllEntities.at(0)->PrintAll();

This basically calls the function 'PrintAll' from the first Entity(which in this case is 'ent1'), and its supposed to print the same text as this: 'ent1.PrintAll()' but it does not, it prints: (Name: "" with Tag: "" and ID: ""), i thought i was not using the vector correctly but as shown below it doesn't work with pointers too:

lastEntity->PrintAll();

This doesn't work !

I'm not sure what the problem is, and how to fix it, firstly i thought maybe something about the variables: 'Name' and 'Tag' and 'ID' doesn't work, but the problem is that the vector doesn't point to the variable, i've tried with lots of different functions, not only with printing name, but it doesn't 'track' the Entity that was created !

12
  • This doesn't really make sense that it would compile. Is lastEntity a member variable of Entity? What about AllEntities? Show us you're "entity.h" Commented Sep 1, 2016 at 19:08
  • i did not use the entire code, because the other code doesn't matter for my specific question ! ouh and i forgot to add lastEntity, i will edit it Commented Sep 1, 2016 at 19:10
  • What I'm asking is, where are those two variables defined. Are they global variables, or are they declared within your class, like 'class Entity { ... vector <Entity*> AllEntities; Entity* lastEntity; ... }' Commented Sep 1, 2016 at 19:15
  • obviously global. Commented Sep 1, 2016 at 19:19
  • do me a favor. Print the this pointer inside the function. Print the lastentiry pointer. Are they identical ? Commented Sep 1, 2016 at 19:23

1 Answer 1

1

Change the Entity ent1 = Entity("ent1"); to Entity ent1("ent1"). This will properly construct your object.

Sign up to request clarification or add additional context in comments.

15 Comments

can you please explain a little why do i have to do that ?
@Drinkadriu yes the statement Entity("ent1") creates a temporary object of type Entity which is then copied (using the copy constructor which you have NOT defined). You end up with a bad constructed object. The Entity ent1("ent1") properly calls the constructor.
@Drinkadriu: What happens if you make 2 entities and try to print both of them? I fear that it will not work correctly. Post a minimal reproducible example as I requested.
@AndyG it will work if the objects are created properly. I am pretty sure that my guess is correct. Unfortunately, we can't be 100% sure without knowing all the class details.
@Kostas: Well... maybe with an old compiler, or a new one with copy elision explicitly turned off.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.