//Using g++ and ubuntu.
#include <vector>
using namespace std;
定义一个类:C++有构造函数的类向量
class foo(){
(...)
foo(int arg1, double arg2);
}
构造:
foo::foo(int arg1, double arg2){
(...) //arrays whose length depend upon arg1 and arg2
}
我愿做这样的事情:
vector<foo> bar(10); //error: no matching function for call to 'foo::foo()'
bar[0] = new foo(123, 4.56);
(...)
替代方法(我喜欢更少)是使用push_back:
vector<foo> bar; //works
bar.push_back(new foo(123, 4.56)); //throws similar error.
//Omitting the "new" compiles but throws a "double free or corruption (fasttop)" on runtime.
我想要构造不同的向量的不同元素,所以我不想使用“重复序列构造函数”。 应该做什么?
+1的一个很好的答案。如果可以的话,我想给出额外的+1,因为不是说你应该总是使用std :: vector,但是你应该总是使用vector。 – Simon 2010-08-09 06:23:57