Drin kadriu Drin kadriu - 1 year ago 68
C++ Question

pass the reference of this class inside it's constructor to a vector of pointers C++

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'


#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");




// 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 !


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


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:


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 !

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download