实体类的计数器给我未定义符号的错误

时间:2018-12-18 20:34:52

标签: c++

我已经开始学习c ++,现在我将介绍智能指针。 我有一个小类Entity,只是为了查看它们如何在作用域内工作,我在类内创建了一个计数器来跟踪该类有多少实例。

这是代码:

#include <iostream>
#include <memory>

class Entity 
{
private:
    static int count;
public:
    Entity()
    {
        count++;
        std::cout << "Created Entity n"<< std::endl;
    }

    ~Entity()
    {
        count--;
        std::cout << "Destroyed Entity n"<< std::endl;
    }
};

int main()
{
    {
        std::shared_ptr<Entity> e0; 
        {
            std::unique_ptr<Entity> entity = std::make_unique<Entity>();
            std::shared_ptr<Entity> sharedEntity = std::make_shared<Entity>();
            e0 = sharedEntity;
        }
        std::cout << "End of scope" << std::endl;
     }

    std::cout << "Program Ended" << std::endl;
}

问题是给我这个错误,但我不知道为什么。

Undefined symbols for architecture x86_64:
  "Entity::count", referenced from:
      Entity::Entity() in main-711f7c.o
      Entity::~Entity() in main-711f7c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see     invocation)

感谢帮助。

0 个答案:

没有答案
相关问题