"鲍勃" <博*********** @ yahoo.com>在消息中写道 news:11 ********************* @ g14g2000cwa.googlegro ups.com ... 这就是我所拥有的: void miniVector< T> :: insertOrder(miniVector< T>& v,const T& item) { int i,j; T target; vSize + = 1; T newVector; newVector = new T [vSize]; for(i = 0; i< vSize-1; i ++) newVector [i] = v [i]; newVector [vSize] = item; v = newVector; } 为什么你不这样做? void miniVector< T> :: insertOrder(miniVector< T>& v,const T& item) { v.push_back(item); } 感谢您的提示,但仍然没有让我设置v = newVector;。 这是'修改后的代码: 模板< typename T> void miniVector< T> :: insertOrder(miniVector< T> ;& v,const T& item) { int i,j; T target; vSize + = 1; T * newVector = new T [vSize]; for(i = 0; i< vSize-1; i ++) newVector [i] = v [i]; newVector [vSize] = item; v = newVector; delete [] newVector; } 我输错了什么?谢谢! Here''s what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item){int i, j;T target; vSize += 1;T newVector;newVector=new T[vSize];for(i = 0; i < vSize-1; i++)newVector[i] = v[i];newVector[vSize] = item;v = newVector;}The problem is that the compiler won''t let me set "v = newVector;". Iget the following error: error: invalid conversion from ''int*'' to ''int'' What I''m trying to do is create a new vector that is one size largerthan an existing vector in order to add an item. How can I make theexisting vector equal to the new pointer? Thanks! 解决方案 see in context "Bob" <bo***********@yahoo.com> wrote in messagenews:11*********************@g14g2000cwa.googlegro ups.com... Here''s what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T[vSize];^^^^ you are trying to assing to an object of type T a pointer to an arrayof objects of type Ttry this instead:T* newVector = new T[vSize]; for(i = 0; i < vSize-1; i++) newVector[i] = v[i]; newVector[vSize] = item; v = newVector;^^^^^^^^^^^^^^ hope that your miniVector<T> has an assignment operator thataccepts T* as rhs// you also forgot todelete[] newVector; } The problem is that the compiler won''t let me set "v = newVector;". I get the following error: error: invalid conversion from ''int*'' to ''int'' What I''m trying to do is create a new vector that is one size larger than an existing vector in order to add an item. How can I make the existing vector equal to the new pointer? Thanks!I wouldn''t do it the way you chose. Dan"Bob" <bo***********@yahoo.com> wrote in messagenews:11*********************@g14g2000cwa.googlegro ups.com... Here''s what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T[vSize]; for(i = 0; i < vSize-1; i++) newVector[i] = v[i]; newVector[vSize] = item; v = newVector; } Why don''t you just do this? void miniVector<T>::insertOrder(miniVector<T>& v, const T& item){v.push_back(item);}Thanks for the tip, but that still doesn''t let me set "v = newVector;".Here''s the modified code: template <typename T>void miniVector<T>::insertOrder(miniVector<T>& v,const T& item){int i, j;T target; vSize += 1;T* newVector = new T[vSize];for(i = 0; i < vSize-1; i++)newVector[i] = v[i];newVector[vSize] = item;v = newVector;delete [] newVector;} Did I type something wrong? Thanks! 这篇关于如何制作Vector =指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-30 06:06