I am having issues accessing a static property in a class. I am getting the following error:
shape.obj : error LNK2001: unresolved external symbol "public: static class TCollection<class Shape *> Shape::shapes"
The definition of the class is:
class Shape {
public:
static Collection<Shape*> shapes;
static void get_all_instances(Collection<Shape*> &list);
};
And the implementation of the static method being:
void Shape::get_all_instances(Collection<Shape*> &list) {
list = Shape::shapes;
}
It seems like the shapes
property isn't being initialized.
Static properties are used when we'd like to store class-level data, also not bound to an instance. The syntax is: class MyClass { static property = ...; static method() { ... } } Technically, static declaration is the same as assigning to the class itself: MyClass.
In C#, static means something which cannot be instantiated. You cannot create an object of a static class and cannot access static members using an object. C# classes, variables, methods, properties, operators, events, and constructors can be defined as static using the static modifier keyword.
A static class can only have static members — you cannot declare instance members (methods, variables, properties, etc.) in a static class. You can have a static constructor in a static class but you cannot have an instance constructor inside a static class.
A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new operator to create a variable of the class type.
You're right since static variable are only declared within class and not defined.
You must define them too, just add following line into the file where is your implementation.
Collection<Shape*> Shape::shapes;
And It should do the trick.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With