我对c ++很陌生,我试图创建一个系统,我可以将项目声明为项目注册表,以便稍后我可以通过id调用它们。每当我将我的数组指针更改为静态时,程序就无法编译并且错误输出
Error LNK2001 unresolved external symbol "private: static class item * item_registry::m_items" (?m_items@item_registry@@0PAVitem@@A) NewVoyage C:\Users\amazo\documents\visual studio 2015\Projects\NewVoyage\NewVoyage\item_registry.obj 1
item_registry.h
#pragma once
#include "item.h"
class item_registry
{
public:
item_registry();
~item_registry();
static int register_item(item p_item);
static item get_item(int p_id);
static void init();
private:
static int const m_size = 512;
static item *m_items;
};
item_registry.cpp
#include "item_registry.h"
item_registry::item_registry()
{
}
item_registry::~item_registry()
{
}
void item_registry::init()
{
item_registry::m_items = new item[item_registry::m_size];
for (int i = 0; i < item_registry::m_size; i++)
{
item_registry::m_items[i] = item("NULL", -1);
}
}
int item_registry::register_item(item p_item)
{
for (int i = 0; i < item_registry::m_size; i++)
{
if (item_registry::m_items[i].get_name() == "NULL" || item_registry::m_items[i].get_value() == -1)
{
item_registry::m_items[i] = p_item;
return i;
}
}
return -1;
}
item item_registry::get_item(int p_id)
{
return item_registry::m_items[p_id];
}
答案 0 :(得分:1)
声明一个静态成员变量并不意味着定义它。
因此,你必须定义它,比如说
item *item_registry::m_items;
(请注意,此定义中没有使用static
)在item_registry.cpp或某处。