在头文件之外实现静态方法时的未定义参考

时间:2019-05-07 13:17:18

标签: c++

我有一个类class Camera(TimeStampedModel): public_identifier = models.UUIDField(unique=True, default=uuid.uuid4, editable=False) serial_number = models.CharField(max_length=100, unique=True, null=True) name = models.CharField(max_length=50, blank=True, null=True) group = models.ForeignKey('CameraGroup', on_delete=models.SET_NULL, null=True, related_name='cameras', related_query_name='cameras') group_tracker = FieldTracker(fields=('group',)) objects = CameraQuerySet.as_manager() #identifier from assembly (what parts does the camera consist of) bom = models.CharField(max_length=50, blank=True, null=True) #what firmware is the camera on (mender artifact name) firmware = models.CharField(max_length=50, blank=True, null=True) #last seen at ip last_sighting_ip = models.CharField(max_length=50, blank=True, null=True) last_sighting_time = models.DateTimeField(null=True, blank=True) #json blob that the camera can send home w. health status health = models.TextField(blank=True) ,它有一个静态实例成员A
我正在尝试创建一个静态方法A* a,该方法将启动initA()
但是,如果我在a文件而不是头文件中实现该方法,则会收到未定义的引用错误:

头文件的代码:

cpp

class A { public: static A *a; static void initA (); }; 文件的代码:

cpp

编译此代码会产生以下错误(从在线IDE复制。我的VS会产生不同的错误,但含义相同)

#include "A.h"
void A::initA(){
    A::a = new A();
}

请注意,将行/var/tmp/ccW1EJl7.o: In function `A::initA()': A.cpp:(.text+0x11): undefined reference to `A::a' collect2: error: ld returned 1 exit status 替换为A::a = new A();(省略a = new A();)会产生相同的错误。

任何想法我在做什么错,如何正确实施该方法?

0 个答案:

没有答案