1
我正在使用模板對象作爲向量參數的項目。我必須嚴格使用對象和任何原始類型。我正在製作一個較小的示例來幫助我掌握更大的圖景。模板對象向量
到目前爲止,這裏是我有:
#include <iostream>
#include <vector>
using namespace std;
template <class T>
class Thing {
public:
Thing(T type) {
memVar = type;
}
T getMemVar() {
return memVar;
}
private:
T memVar;
};
class U {
public:
U(int i) {
j = i;
}
int getJ() {
return j;
}
private:
int j;
};
int main() {
// your code goes here
vector < Thing <U> > v;
v.push_back(); // idk how to add new elements to this vector.
// I've tried: v.push_back(Thing <U> i(U)),
// v.push_back(U obj(4)), etc etc...
return 0;
}
我不知道如何將元素添加到這個矢量。
它的工作原理複製
type
!非常感謝您對此的見解。我花了3天的時間試圖弄清楚這一點。我無法表達我多麼感恩。 –@TheoHandy - 你很好;如果你可以編譯C++ 11或更新的版本,改進了anser。 – max66