gpt4 book ai didi

c++ - 模板类中的静态变量导致 C2371 'redefinition; different basic types'

转载 作者:行者123 更新时间:2023-11-28 06:36:26 26 4
gpt4 key购买 nike

我有一个带有静态成员变量的模板类。但是,在编译时,出现以下错误:

C2371: 'S_TYPES' : redefinition; different basic types

这是有问题的类:

// This all is in Type.h

template<class T>
class Type
{
private:
static std::map<unsigned int, Type<T>> S_TYPES;
};

template<class T>
std::map<unsigned int, Type<T>> Type<T>::S_TYPES;

如何修复这里的错误?

最佳答案

OP 一看到错误就编辑了问题:您定义的类型与声明的类型不同:

template<class T>
class Type
{
private:
static std::map<unsigned int, Type<T>> S_TYPES;
};

template<class T>
std::map<unsigned int, Type<T>, TypeInfoComparator> Type<T>::S_TYPES; // different

你应该确保它们匹配:

template<class T>
class Type
{
private:
static std::map<unsigned int, Type<T>> S_TYPES;
};

template<class T>
std::map<unsigned int, Type<T>> Type<T>::S_TYPES;

关于c++ - 模板类中的静态变量导致 C2371 'redefinition; different basic types',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26699382/

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