gpt4 book ai didi

类成员列表的 C++ 模板构造

转载 作者:行者123 更新时间:2023-11-30 05:28:13 39 4
gpt4 key购买 nike

所以我之前制作了一些 C++ 模板:curiously recurring template pattern 的一些非常基本的示例对于数学数字类。这次我尝试使用相同的模式为类的对象创建列表或“网络”,它们可以将自己添加到其中并四处寻找其他对象。我想我可以使用静态 std::list 来做到这一点。所以我的尝试是这样的:

template<class C> class HasNetwork{
public:
HasNetwork(){}
static list<C*> m;
};
template<class C> list<C*> HasNetwork<C>::m = list<C*>();
class IHaveNetwork: public HasNetwork<IHaveNetwork>{
public:
IHaveNetwork(){ m.push_back(this); }
};
int main(){
IHaveNetwork lHF, lHF2;
//for(list<IHaveNetwork*>::iterator it = lHF.m->begin(); ; it++);
return 0;
}

它似乎可以编译,但我遇到了严重的链接错误(即使 for 循环迭代器被注释掉了)。也许我需要做一些强制转换,或者直到构造函数完成后才定义“this”?

这是链接错误:

/tmp/templatest-912860.o: In function `__clang_call_terminate':
templatest.cpp:

(.text.__clang_call_terminate[__clang_call_terminate]+0x9): undefined reference to `__cxa_begin_catch'


templatest.cpp:(.text.__clang_call_terminate[__clang_call_terminate]+0x12): undefined reference to `std::terminate()'


/tmp/templatest-912860.o: In function `__gnu_cxx::new_allocator<std::_List_node<IHaveNetwork*>

>::deallocate(std::_List_node<IHaveNetwork*>*, unsigned long)':

模板测试.cpp:

 (.text._ZN9__gnu_cxx13new_allocatorISt10_List_nodeIP12IHaveNetworkEE10deallocateEPS4_m[_ZN9__gnu_cxx13new_allocatorISt10_List_nodeIP12IHaveNetworkEE10deallocateEPS4_m]+0x1c): undefined reference to `operator delete(void*)'

/tmp/templatest-912860.o: In function `std::list<IHaveNetwork*, std::allocator<IHaveNetwork*>

>::_M_insert(std::_List_iterator<IHaveNetwork*>, IHaveNetwork* const&)':

模板测试.cpp:

(.text._ZNSt4listIP12IHaveNetworkSaIS1_EE9_M_insertESt14_List_iteratorIS1_ERKS1_[_ZNSt4listIP12IHaveNetworkSaIS1_EE9_M_insertESt14_List_iteratorIS1_ERKS1_]+0x31): undefined reference to 

`std::__detail::_List_node_base::_M_hook(std::__detail::_List_node_base*)'

/tmp/templatest-912860.o:在函数“std::list >::_M_create_node(IHaveNetwork* const&)”中:模板.cpp:

(.text._ZNSt4listIP12IHaveNetworkSaIS1_EE14_M_create_nodeERKS1_[_ZNSt4listIP12IHaveNetworkSaIS1_EE14_M_create_nodeERKS1_]+0xa0): undefined reference to `__cxa_begin_catch'

templatest.cpp:(.text._ZNSt4listIP12IHaveNetworkSaIS1_EE14_M_create_nodeERKS1_[_ZNSt4listIP12IHaveNetworkSaIS1_EE14_M_create_nodeERKS1_]+0xbb): undefined reference to `__cxa_rethrow'
templatest.cpp:

(.text._ZNSt4listIP12IHaveNetworkSaIS1_EE14_M_create_nodeERKS1_[_ZNSt4listIP12IHaveNetworkSaIS1_EE14_M_create_nodeERKS1_]+0xce): undefined reference to `__cxa_end_catch'

/tmp/templatest-912860.o: In function `__gnu_cxx::new_allocator<std::_List_node<IHaveNetwork*> >::allocate(unsigned long, void const*)':

templatest.cpp:(.text._ZN9__gnu_cxx13new_allocatorISt10_List_nodeIP12IHaveNetworkEE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorISt10_List_nodeIP12IHaveNetworkEE8allocateEmPKv]+0x33): undefined reference to `std::__throw_bad_alloc()'

templatest.cpp:(.text._ZN9__gnu_cxx13new_allocatorISt10_List_nodeIP12IHaveNetworkEE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorISt10_List_nodeIP12IHaveNetworkEE8allocateEmPKv]+0x40): undefined reference to `operator new(unsigned long)'

/tmp/templatest-912860.o:(.eh_frame+0x13): 对 `__gxx_personality_v0' 的 undefined reference

最佳答案

我无法复制。

这是你的代码的一个稍微修改过的版本,它编译链接并在没有警告的情况下运行(我只需要修复一些间接级别的错误):

#include <iostream>
#include <list>
#include <string>

template<class C> class HasNetwork{
public:
HasNetwork(){}
static std::list<C*> m;
};
template<class C> std::list<C*> HasNetwork<C>::m = std::list<C*>();
class IHaveNetwork: public HasNetwork<IHaveNetwork>{
std::string name;
public:
IHaveNetwork(const std::string &name): name(name) { m.push_back(this); }
std::string getName() const {
return name;
}
};
int main(){
IHaveNetwork lHF("foo"), lHF2("bar");
for(std::list<IHaveNetwork*>::iterator it = lHF.m.begin(); it != lHF.m.end(); it++) {
std::cout << (*it)->getName() << std::endl;
}
return 0;
}

关于类成员列表的 C++ 模板构造,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36934667/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com