I am currently working on an instance system, so far this is something I tried:
//[...]
#include <string>
#include <list>
class Instance { // Instance class
public:
std::string Name;
std::string Type;
SDL_Texture* Texture; // All this is for a list for an SDL renderer.
int x;
int y;
};
class instances {
public:
Instance New(std::string Name, std::string Type) {
Instance newInstance; // Clone the class? Doesn't raise error.
Instance::Name = "Test"; // Set a variable from the cloned class. Error: "a nonstatic member reference must be relative to a specific object"
};
std::list<int> list = {}; // Ignore this...
};
//[...]
You will be able create instances with the "New" function shown above, which should return a copy of the "Instance" class (also above).
Unfortunately it raises an error as I wrote in the script comments.
>Solution :
The problem is that Name is a non-static data member of class Instance so you would need an object of type Instance for which you can access that field Name.
Additionally you should add a return statement inside the function New.
The correct way would be as shown below:
Instance New(std::string Name, std::string Type) {
Instance newInstance; // create object of type Instance
//-----------------v--------------------->used . instead of ::
newInstance.Name = "Test";
return newInstance;//added return statement
};