参数化多态性中的静态成员声明
Static member declaration in Parametrized polymorphism
我有性别 class,男性和女性作为我 class 的参数类型
我正在使用以下层次结构:
#ifndef __GENDER_H
#define __GENDER_H
#include <string>
using namespace std;
// Forward declaration of templatized class
template<typename T>
class GenderTypes; // Generic Gender type to generate specific genders
// Generic gender type
class Gender { // Abstract Base Class
const string& name_; // Name of the Gender
struct MaleType {};
struct FemaleType {};
protected:
Gender(const string& name) : name_(name) {}
virtual ~Gender() { }
public:
const string& GetName() const { return name_; }
bool IsMale(const Gender&); // Checking and matching gender
// Enumerated types - the target sub-types
typedef GenderTypes<MaleType> Male;
typedef GenderTypes<FemaleType> Female;
};
// Specific gender types
template<typename T>
class GenderTypes : public Gender {
static const string sName;
GenderTypes(const string& name = GenderTypes<T>::sName) : Gender(name) { }
~GenderTypes() { }
public:
// Singleton object - placeholder for the respective type
static const GenderTypes<T>& Type() {
static const GenderTypes<T> theObject; // May be non-const for changeable behavior
return theObject;
}
};
inline bool Gender::IsMale(const Gender& g) { return &g == &Gender::Male::Type(); }
#endif
并声明静态成员name_如下:
#include <string>
using namespace std;
#include "../inc/gender.h"
// Names defined as static constants
const string Gender::Male::sName = "Male";
const string Gender::Female::sName = "Female";
这种层次感很好。那么为什么编译器会给出这个错误:
gender.cpp:5:14: error: specializing member ‘GenderTypes<Gender::MaleType>::sName’ requires ‘template<>’ syntax
5 | const string Gender::Male::sName = "Male";
我应该如何初始化这个静态数据?
我正在使用 VS CODE 编辑器和 Ubuntu 20.04
在你的 .cpp 中做这个小改动,它在我的机器上编译(和工作)正常:
template<>
const string Gender::Male::sName = "Male";
template<>
const string Gender::Female::sName = "Female";
确实在您的 header 中您应该使用
#ifndef GENDER_H
#define GENDER_H
[...]
#endif
或
#pragma once
[...]
我有性别 class,男性和女性作为我 class 的参数类型 我正在使用以下层次结构:
#ifndef __GENDER_H
#define __GENDER_H
#include <string>
using namespace std;
// Forward declaration of templatized class
template<typename T>
class GenderTypes; // Generic Gender type to generate specific genders
// Generic gender type
class Gender { // Abstract Base Class
const string& name_; // Name of the Gender
struct MaleType {};
struct FemaleType {};
protected:
Gender(const string& name) : name_(name) {}
virtual ~Gender() { }
public:
const string& GetName() const { return name_; }
bool IsMale(const Gender&); // Checking and matching gender
// Enumerated types - the target sub-types
typedef GenderTypes<MaleType> Male;
typedef GenderTypes<FemaleType> Female;
};
// Specific gender types
template<typename T>
class GenderTypes : public Gender {
static const string sName;
GenderTypes(const string& name = GenderTypes<T>::sName) : Gender(name) { }
~GenderTypes() { }
public:
// Singleton object - placeholder for the respective type
static const GenderTypes<T>& Type() {
static const GenderTypes<T> theObject; // May be non-const for changeable behavior
return theObject;
}
};
inline bool Gender::IsMale(const Gender& g) { return &g == &Gender::Male::Type(); }
#endif
并声明静态成员name_如下:
#include <string>
using namespace std;
#include "../inc/gender.h"
// Names defined as static constants
const string Gender::Male::sName = "Male";
const string Gender::Female::sName = "Female";
这种层次感很好。那么为什么编译器会给出这个错误:
gender.cpp:5:14: error: specializing member ‘GenderTypes<Gender::MaleType>::sName’ requires ‘template<>’ syntax
5 | const string Gender::Male::sName = "Male";
我应该如何初始化这个静态数据? 我正在使用 VS CODE 编辑器和 Ubuntu 20.04
在你的 .cpp 中做这个小改动,它在我的机器上编译(和工作)正常:
template<>
const string Gender::Male::sName = "Male";
template<>
const string Gender::Female::sName = "Female";
确实在您的 header 中您应该使用
#ifndef GENDER_H
#define GENDER_H
[...]
#endif
或
#pragma once
[...]