如何制作Vector =指针 [英] How To Make Vector = Pointer

查看:61
本文介绍了如何制作Vector =指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我所拥有的:


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;

}

问题是编译器不允许我设置v = newVector;。我b $ b得到以下错误:


错误:无效转换为''int *''到''int''

我要做的是创建一个比现有向量大一号的新向量

以添加项目。如何使

现有向量等于新指针?谢谢!

解决方案

参见上下文


" Bob" <博*********** @ 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];
^^^^你试图向类型为T的对象提供一个指向数组的指针

类型为T的对象
试试这个:

T * newVector = new T [vSize];

for(i = 0; i< vSize-1; i ++)
newVector [i] = v [i];
newVector [vSize] = item;
v = newVector;
^^^^^^^^^^^^^^希望你的miniVector< T>有一个赋值运算符

接受T *为rhs

//你也忘了

delete [] newVector;

}

问题是编译器不允许我设置v = newVector;。我收到以下错误:

错误:从''int *''无效转换为''int''

我想要的是什么do是为了添加项目,创建一个比现有向量大一个的新向量。如何使
现有向量等于新指针?谢谢!



我不会按你选择的方式去做。


Dan

"鲍勃" <博*********** @ yahoo.com>在消息中写道

news:11 ********************* @ g14g2000cwa.googlegro ups.com ...
< blockquote class =post_quotes>这就是我所拥有的:

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;". 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!

解决方案

see in context

"Bob" <bo***********@yahoo.com> wrote in message
news: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 array
of objects of type T
try 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 that
accepts T* as rhs
// you also forgot to
delete[] 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 message
news: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 =指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆