c++ - 在模板类中使用模板方法

标签 c++ c++11 templates

为什么它需要 Struct1 以及如何格式化它以便我可以修改任何结构中的属性?

我有 1 个模板类用于修改包含相同属性名称的 3 个结构中的数据,它们只是碰巧使用了不同的数据类型。

我正在尝试在我的模板类中使用模板方法,在各种实例化时,一切似乎都像我预期的那样与模板类一起工作。

GenericClass<Struct1> inventory[1000];
GenericClass<Struct2> machines[1000];
GenericClass<Struct3> physicalStructures[1000];

(是的,我知道 vector 存在,一起玩,我包含尽可能少的代码)也就是说,当我尝试更改结构的一个属性时,我收到一条错误消息,指出它期待struct,而不是采用我交给它的任何数据类型。

Main.cpp:39:28: error: no viable conversion from 'int' to 'Struct1'
inventory[0].setIdentifier(1);

我已经检查了这 3 个引用文献(以及更多),但它们似乎都没有做我在其中任何一个中所做的事情。 How to define template function within template class in *.inl file

Template class with template function

https://isocpp.org/wiki/faq/templates

这是我的代码示例。

template <class Type>
class GenericClass
{ 
private:
  Type Identifier;
public:
  void setIdentifier(Type Param);
  Type getIdentifier();
};


/* Struct prototypes
************************/
struct Struct1
{
private:
  int Identifier;
  string Description;
  float Value;
}

struct Struct2
{
private:
  long int Identifier;
  string Description;
  float Value;
};

struct Struct3
{
private:
 string Identifier;
 string Description;
 double Value;
};


int main()
{
  GenericRecord<Struct1> inventory[1000];
  GenericRecord<Struct2> machines[1000];
  GenericRecord<Struct3> physicalStructures[1000];
  inventory[0].setIdentifier(1);
}


template <class Type>
void GenericRecord<Type>::setIdentifier(Type Param)
{
  Identifier = Param;
}
template <class Type>
Type GenericRecord<Type>::getIdentifier()
{
  return Identifier;
}

return 方法也不起作用,我预计它们都因类似原因而失败。

同样,为什么它需要 Struct1 以及如何格式化它以便我可以修改任何结构中的属性?

最佳答案

Why is it expecting Struct1

因为这是它被告知要使用的类型。

给定模板:

template<typename Type>
class GenericClass
{
private:
    Type Identifier;

public:
    void setIdentifier(Type Param);
    Type getIdentifier();
};

类型为Struct1的实例化( GenericClass<Struct1> ) 生成以下实现:

// Just for illustration
class GenericClassStruct1
{
private:
    Struct1 Identifier;

public:
    void setIdentifier(Struct1 Param);
    Struct1 getIdentifier();
};

上面的类已经替换了所有出现的 TypeStruct1 .现在,应该很容易明白为什么调用 setIdentifier 了。期望一个 Struct1而不是 int .

how do I format it so I can modify the properties in any struct

有不止一种方法可以做到这一点。什么是“正确”方法将取决于您的问题的限制,但以下示例显示了一种方法。

例子

Working Example

#include <iostream>

// Declared but not defined, specializations will provide definitions
template<typename T>
struct GenericTypeTraits;

template<typename T>
class Generic
{
public:
    using IdType = typename GenericTypeTraits<T>::IdType;

    void setIdentifier(IdType id)
    {
        mType.mIdentifier = id;
    }

    IdType getIdentifier() const
    {
        return mType.mIdentifier;
    }

private:
    T mType;
};

class Type1
{
public:
    int mIdentifier;
};

template<>
struct GenericTypeTraits<Type1>
{
    using IdType = int;
};

class Type2
{
public:
    std::string mIdentifier;
};

template<>
struct GenericTypeTraits<Type2>
{
    using IdType = std::string;
};

int main()
{
    Generic<Type1> t1[5];
    t1[0].setIdentifier(3);
    std::cout << t1[0].getIdentifier() << "\n";

    Generic<Type2> t2[5];
    t2[0].setIdentifier("3");
    std::cout << t2[0].getIdentifier() << "\n";

    return 0;
}

输出

3
3

关于c++ - 在模板类中使用模板方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46984733/

相关文章:

c++ - 在单个 Raspberry-Pi 上使用 MPI

c++ - 在 C++ 版本的 tensorflow 上使用多个 gpus

堆栈内存中的c++ lambda

c++ - 如何将 std::vector 转换为 Eigen 中的矩阵?

c++ - gets() 未在范围内声明

c++ - 如何在配置主要项目时构建 cmake ExternalProject?

c++ - 带有类模板的可变参数模板的特化

javascript - 如何突出显示网站菜单上的事件选项卡?

c++ - 如果基类是成员函数的参数类型,是否需要指定基类的模板参数?

c++ - 如何解决从 Point<T> 到非标量 Point<U> 的转换错误