Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Static Property

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.

like image 200
Louis Avatar asked Oct 24 '10 07:10

Louis


People also ask

What are static properties?

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.

What is a static property C#?

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.

Can a static class have a property?

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.

What is static class in c3?

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.


1 Answers

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.

like image 112
Keynslug Avatar answered Oct 01 '22 14:10

Keynslug