如何根据模板类型拥有对象向量
C++ How to have a vector of objects depending on template type
我有一个模板类C,它应该包含一个具有E对象的向量。vector中的E对象将存储与模板相同的类型。所以如果C对象是int
类型的,向量应该包含E个包含int
的对象,如果C有double
,那么E应该存储一个double
,等等。代码看起来像这样:
template<class T> class C {
public:
class E {
private:
T value; //if C has type int then the events should store int
std::string description;
public:
Event(std::string desc, T val) {
this->description = desc;
this->value = val;
}
};
C();
virtual ~C();
bool add_e (std::string description);
private:
T c_value;
std::vector<E> c_vector; //this should look somewhat like [E<int>, E<int>, E<int>]
};
这个可以编译,但是当我尝试像这样在C.cpp文件中向c_vector添加元素时:
template<class T>
bool C<T>::add_e(std::string description) {
c_vector.add(E(description, c_value));
}
我得到错误:'class std::vector
当我尝试搜索web时,我只找到关于在vector中存储不同模板类型的信息,例如使E成为抽象类的派生类,然后将抽象类指针存储在vector中,但我希望所有元素都包含相同的类型。
使用push_back()
from std::vector
相关文章:
- 是否可以获取成员函数模板参数的拥有对象?
- 拥有或在对象之间共享资源
- googlemock-如何模拟被测试类所拥有的对象
- C++ shared_ptr调用拥有对象的解构函数
- 在另一个 QThread 上运行成员方法时,无法将事件发送到其他线程拥有的对象
- 使用 lambda/std::function 删除拥有该函数的对象
- QT:QT无法将事件发送到另一个线程拥有的对象 - 原因
- 当 std::unique_ptr 在手动删除其拥有的对象后超出范围时,它如何工作?
- 谁拥有QQMlinCubator返回的对象
- 带有 std::map 的 c++ 结构是拥有动态数据对象的好设计吗?
- 是否可以使用对shared_ptr拥有的对象的引用
- 将原始指针向量清除给由独特指针拥有的对象
- 如何将 QEvent 传递给接收者拥有的对象?
- 如何正确地使一个对象拥有另一个多态对象
- C++拥有持久对象的最佳方式
- 将类的实例传递给另一个构造函数,该构造函数将其对象添加到所传递实例所拥有的列表中
- C++正在更改子类中拥有的对象类型
- 如何将此的shared_ptr返回给拥有它的对象
- 类同时拥有和共享对象
- 如何根据模板类型拥有对象向量