C++ 多个错误未定义对方法的引用

C++ 多个错误未定义对方法的引用,c++,C++,我有这样的头文件声明 #ifndef CONTAINER_H #define CONTAINER_H #include <map> #include "object.h" namespace memory { template<class T> class Collector { }; template <class T> class Collector<T*> { private: std::map<std::string

我有这样的头文件声明

#ifndef CONTAINER_H
#define CONTAINER_H

#include <map>
#include "object.h"

namespace memory {

template<class T>
class Collector {
};

template <class T>
class Collector<T*> {

private:
    std::map<std::string, T*> mem_storage;
public:
    std::string put_object(T* to_store);
    T* get_object(std::string key);
    void list_objects();
    void clean_up();
    void clean_object(std::string key); // force cleaning
    void mark_object_to_remove(std::string key); // mark unused
    void clean_removable(); // move to scheduled task

    ~Collector() {};
};

}

#endif
\ifndef集装箱
#定义容器
#包括
#包括“object.h”
名称空间内存{
模板
类收集器{
};
模板
类收集器{
私人:
标准::地图内存存储;
公众:
std::字符串put_对象(T*到_存储);
T*get_对象(std::字符串键);
void list_objects();
无效清除();
void clean_对象(std::string key);//强制清理
void mark_object_to_remove(std::string key);//标记未使用
void clean_removable();//移动到计划任务
~Collector(){};
};
}
#恩迪夫
以及这样的实施

#include "../headers/container.h"

#define null nullptr

#include <iostream>
#include <string>

#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/lexical_cast.hpp>

using namespace memory;

template<typename T>
void Collector<T*>::clean_object(std::string key) {
    int removed = mem_storage.erase(key);
    std::string removed_string = boost::lexical_cast<std::string>(removed);
}

template<typename T>
void Collector<T*>::clean_up() {
    mem_storage.clear();
}

template<typename T>
T* Collector<T*>::get_object(std::string key) {
    typename std::map<std::string, T*>::iterator it = mem_storage.find(key);
    if (it != mem_storage.end()) {
        return it->second;
    } else {
        return null;
    }
}

template<typename T>
void Collector<T*>::list_objects() {
    for (typename std::map<std::string, T*>::iterator it = mem_storage.begin(); it != mem_storage.end(); ++it) {
        std::cout << it->first << " => " << it->second->to_string() << std::endl;
    }
}

template<typename T>
std::string Collector<T*>::put_object(T* to_store) {
    boost::uuids::uuid uuid = boost::uuids::random_generator()();
    std::string key = boost::uuids::to_string(uuid);
    mem_storage[key] = to_store;
    return key;
}

template<typename T>
void Collector<T*>::mark_object_to_remove(std::string key) {
    mem_storage.find(key)->second->removable = true;
}

template<typename T>
void Collector<T*>::clean_removable() {
    typename std::map<std::string, T*>::iterator it = mem_storage.begin();
    while (it != mem_storage.end()) {
        std::string id = it->first;
        T instance = it->second;
        if (instance->removable) {
            mem_storage.erase(it++);
        } else {
            ++it;
        }
    }
}
#包括“./headers/container.h”
#定义空空空ptr
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间内存;
模板
void收集器::clean_对象(std::字符串键){
int removed=mem_storage.erase(键);
std::string removed\u string=boost::lexical\u cast(removed);
}
模板
空收集器::清除(){
mem_storage.clear();
}
模板
T*收集器::获取对象(std::字符串键){
typename std::map::iterator it=mem_storage.find(key);
if(it!=mem_storage.end()){
返回->秒;
}否则{
返回null;
}
}
模板
void收集器::列出_对象(){
for(typename std::map::iterator it=mem_storage.begin();it!=mem_storage.end();+it){
std::cout first to_string()second->removable=true;
}
模板
空收集器::clean_removable(){
typename std::map::iterator it=mem_storage.begin();
while(it!=mem_storage.end()){
std::string id=it->first;
T instance=it->second;
如果(实例->可移动){
mem_storage.erase(it++);
}否则{
++它;
}
}
}
我的主文件如下所示

#include <iostream>

#define byte unsigned char
#define null nullptr

#include "base/headers/object.h"
#include "base/headers/indexed_object.h"
#include "base/headers/container.h"
#include "base/headers/reflector.h"

void garbage_collection_prototype_example() {
    core::Object *io = runtime::RuntimeReflector::create_instance(
        "core::IndexedObject");
    core::Object *io_new = runtime::RuntimeReflector::create_instance("core::IndexedObject");
    std::cout << "Class 1 name: [" + io->get_class_name() + "]" << std::endl;
    std::cout << "Class 2 name: [" + io_new->get_class_name() + "]" << std::endl;

    bool is_same = io_new->is_same_instance(io);
    std::cout << "Is same: " << std::boolalpha << is_same << std::endl;

    memory::Collector<core::Object*> *garbage_collector = new memory::Collector<core::Object*>();
    std::string key = garbage_collector->put_object(io);
    std::string key_new = garbage_collector->put_object(io_new);
    garbage_collector->list_objects();

    garbage_collector->mark_object_to_remove(key);
    garbage_collector->clean_removable();

    garbage_collector->list_objects();

    garbage_collector->clean_up();
}

int main(int argc, char *argv[]) {
    garbage_collection_prototype_example();
    return 0;
}
#包括
#定义字节无符号字符
#定义空空空ptr
#包括“base/headers/object.h”
#包括“base/headers/index_object.h”
#包括“base/headers/container.h”
#包括“底座/封头/反射器.h”
无效垃圾收集原型示例(){
core::Object*io=runtime::RuntimeReflector::create_实例(
“核心:IndexedObject”);
core::Object*io_new=runtime::RuntimeReflector::create_实例(“core::IndexedObject”);
std::无法获取_class_name()+“]”clean_removable();
垃圾收集器->列出垃圾对象();
垃圾收集器->清理();
}
int main(int argc,char*argv[]){
垃圾收集原型示例();
返回0;
}
但当我尝试构建项目时,我有很多错误

D:\c++\eclipse_workspace\SDLAttempts\Debug/../src/main.cpp:22: undefined reference to `memory::Collector<core::Object*>::put_object(core::Object*)'
D:\c++\eclipse_workspace\SDLAttempts\Debug/../src/main.cpp:23: undefined reference to `memory::Collector<core::Object*>::put_object(core::Object*)'
D:\c++\eclipse_workspace\SDLAttempts\Debug/../src/main.cpp:24: undefined reference to `memory::Collector<core::Object*>::list_objects()'
D:\c++\eclipse_workspace\SDLAttempts\Debug/../src/main.cpp:26: undefined reference to `memory::Collector<core::Object*>::mark_object_to_remove(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
D:\c++\eclipse_workspace\SDLAttempts\Debug/../src/main.cpp:27: undefined reference to `memory::Collector<core::Object*>::clean_removable()'
D:\c++\eclipse_workspace\SDLAttempts\Debug/../src/main.cpp:29: undefined reference to `memory::Collector<core::Object*>::list_objects()'
D:\c++\eclipse_workspace\SDLAttempts\Debug/../src/main.cpp:31: undefined reference to `memory::Collector<core::Object*>::clean_up()'
D:\c++\eclipse\u workspace\sdlattests\Debug/。/src/main.cpp:22:对“memory::Collector::put\u object(core::object*)”的未定义引用
D:\c++\eclipse\u workspace\sdlattests\Debug/。/src/main.cpp:23:对“memory::Collector::put\u object(core::object*)”的未定义引用
D:\c++\eclipse\u workspace\sdlattests\Debug/。/src/main.cpp:24:对“memory::Collector::list_objects()”的未定义引用
D:\c++\eclipse\u workspace\sdlattests\Debug/。/src/main.cpp:26:对“内存::收集器::标记对象”的未定义引用要删除(std::uuucx11::基本字符串)
D:\c++\eclipse\u workspace\sdlattests\Debug/。/src/main.cpp:27:对“memory::Collector::clean\u removable()”的未定义引用
D:\c++\eclipse\u workspace\sdlattests\Debug/。/src/main.cpp:29:对“memory::Collector::list_objects()”的未定义引用
D:\c++\eclipse\u workspace\sdlattests\Debug/。/src/main.cpp:31:对“memory::Collector::clean\u up()”的未定义引用
就像它看不到
收集器的任何方法一样。

这可能是因为我对非专门化的基类
收集器的声明是空的吗?但我试图添加这些方法的实现,但没有起到任何作用。

简短回答

不能将函数模板的定义(实现)放入
.cpp
文件中。请将它们放入声明它们的相同标头中

解释


这是因为函数模板在(where)时被实例化它们是被使用的,因此编译器需要查看它们在任何地方是如何定义的。有一些解决方法可以将定义与函数/类模板的声明分开,但所有这些都意味着将模板的定义包含到使用它的翻译单元中。

可能存在这样的重复我必须在
.h
文件或
.hpp
中实现方法,或者在
.cpp
中实现类,但是对于特定的模板参数?@user1432980,您可以将函数模板定义放入container.h,或者创建一个新的头,比如container_impl.h,将定义放在那里,并将其包含在cpp的底部如果您不想“公开”所有的实现细节或最小化范围污染,那么这将非常有用。