带有模板成员共享地址的对象列表,为什么??? [英] A list of objects with a template member shares address, why ???

查看:43
本文介绍了带有模板成员共享地址的对象列表,为什么???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我上面有一点问题(没有找到任何解决方案

www)。我有一个对象列表(粒子)我有一个成员

模板向量(来自TNT的array1d)。我在

构造函数中初始化这些成员,但是当我将不同的向量值写入不同的

粒子时,值变得相同。


我发现每个粒子向量都指向相同的

地址:o(

当我使用数组而不是模板时,那里没有问题。

我是否初始化模板向量,因此每个粒子都有自己的

模板向量?


这里有一些代码片段,这可能更容易理解

比我的漫无边际:


typedef TNT :: Array1D< double> dVector3;

// typedef double dVector3 [3];

enum orient {eHoris,eVertic};


// ---------- -------------------

typedef struct sParticle {

dVector3 position; //粒子的位置
sParticle():位置(3)

{

}

}


list< sParticle> particlePool;

particlePool.resize(noParticles);

for(k = 0; k< emitNoParticles; k ++){

(* emit_iterator).position [0] = k ++

}


这在我使用我的数组向量时有效,但在使用Array1D

向量时,位置向量指向所有

粒子的地址相同。


如何确保每个粒子都有自己的向量?


感谢您的帮助


Dennis

Hi

I have a little problem as stated above (haven''t found any solution on
the www). I have a list of objects (particles) where I have a member
of a template vector (array1d from TNT). I initialise these members in
the constructor, but when I write different vector values to different
particles the values becomes the same.

I have found out that each particles vector are pointing to the same
address :o(
When I used an array instead of a template there where no problem. How
do I initialise the template vector so each particle has it''s own
template vector ??

Here are some snippets of code, which might be more understandable
than my rambling:

typedef TNT::Array1D<double> dVector3;
//typedef double dVector3[3];
enum orient {eHoris, eVertic};

//-----------------------------
typedef struct sParticle{
dVector3 position; //position of particle
sParticle():position(3)
{
}
}

list<sParticle> particlePool;
particlePool.resize(noParticles);
for(k=0;k<emitNoParticles;k++){
(*emit_iterator).position[0] = k++
}

this works when i use my array vector, but when using the Array1D
vector the position vector points to the same address for all
particles.

How do I ensure that each particle has its own vector ?

Thanks for your help

Dennis

推荐答案



"丹尼斯" < DL ******* @ control.auc.dk>在留言中写道

news:ud *********** @ control.auc.dk ...

"Dennis" <dl*******@control.auc.dk> wrote in message
news:ud***********@control.auc.dk...

我有一个如上所述的小问题(没有找到任何解决方案www /)。我有一个对象列表(粒子),我有一个模板向量的成员(来自TNT的array1d)。我在构造函数中初始化了这些成员,但是当我将不同的向量值写入不同的粒子时,值变得相同。

我发现每个粒子向量都指向到同一个地址:o(
当我使用数组而不是模板时没有问题。我如何初始化模板向量,以便每个粒子都拥有它自己的
模板向量??

这里有一些代码片段,可能比我的漫无边际更容易理解:

typedef TNT :: Array1D< double> ; dVector3;
// typedef double dVector3 [3];
enum orient {eHoris,eVertic};

// ------------ -----------------
typedef struct sParticle {
dVector3 position; //粒子的位置
sParticle():position(3)
{
}
}
列出< sParticle> particlePool;
particlePool.resize(noParticles);
for(k = 0; k< ; emitNoParticles; k ++){
(* emit_iterator) .position [0] = k ++
}
当我使用我的数组向量时这是有效的,但是当使用Array1D
向量时,位置向量指向所有<的相同地址br />

如何确保每个粒子都有自己的向量?
Hi

I have a little problem as stated above (haven''t found any solution on
the www). I have a list of objects (particles) where I have a member
of a template vector (array1d from TNT). I initialise these members in
the constructor, but when I write different vector values to different
particles the values becomes the same.

I have found out that each particles vector are pointing to the same
address :o(
When I used an array instead of a template there where no problem. How
do I initialise the template vector so each particle has it''s own
template vector ??

Here are some snippets of code, which might be more understandable
than my rambling:

typedef TNT::Array1D<double> dVector3;
//typedef double dVector3[3];
enum orient {eHoris, eVertic};

//-----------------------------
typedef struct sParticle{
dVector3 position; //position of particle
sParticle():position(3)
{
}
}

list<sParticle> particlePool;
particlePool.resize(noParticles);
for(k=0;k<emitNoParticles;k++){
(*emit_iterator).position[0] = k++
}

this works when i use my array vector, but when using the Array1D
vector the position vector points to the same address for all
particles.

How do I ensure that each particle has its own vector ?




以下列出了未定义的内容在您发布的代码中。


TNT :: Array1D< double>

noParticles

emitNoParticles

emit_iterator


我无法回答这个问题,因为这些事情是未定义的,因此

我无法理解你的代码。


请阅读本文,这些是有关如何发布代码的一些指导原则。按照

这些指导原则,您将很快得到答案。

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8


john



Here are a list of things that are undefined in the code you posted.

TNT::Array1D<double>
noParticles
emitNoParticles
emit_iterator

I cannot answer this question because these things are undefined, therefore
I cannot understand your code any better than you can.

Please read this, these are some guidelines about how to post code. Follow
these guidelines and you will get your question answered quickly.

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

john




" Dennis" < DL ******* @ control.auc.dk>在留言中写道

news:ud *********** @ control.auc.dk ...

"Dennis" <dl*******@control.auc.dk> wrote in message
news:ud***********@control.auc.dk...

我有一个如上所述的小问题(没有找到任何解决方案www /)。我有一个对象列表(粒子),我有一个模板向量的成员(来自TNT的array1d)。我在构造函数中初始化了这些成员,但是当我将不同的向量值写入不同的粒子时,值变得相同。

我发现每个粒子向量都指向到同一个地址:o(
当我使用数组而不是模板时没有问题。我如何初始化模板向量,以便每个粒子都拥有它自己的
模板向量??

这里有一些代码片段,可能比我的漫无边际更容易理解:

typedef TNT :: Array1D< double> ; dVector3;
// typedef double dVector3 [3];
enum orient {eHoris,eVertic};

// ------------ -----------------
typedef struct sParticle {
dVector3 position; //粒子的位置
sParticle():position(3)
{
}
}
列出< sParticle> particlePool;
particlePool.resize(noParticles);
for(k = 0; k< ; emitNoParticles; k ++){
(* emit_iterator) .position [0] = k ++
}
当我使用我的数组向量时这是有效的,但是当使用Array1D
向量时,位置向量指向所有<的相同地址br />

如何确保每个粒子都有自己的向量?
Hi

I have a little problem as stated above (haven''t found any solution on
the www). I have a list of objects (particles) where I have a member
of a template vector (array1d from TNT). I initialise these members in
the constructor, but when I write different vector values to different
particles the values becomes the same.

I have found out that each particles vector are pointing to the same
address :o(
When I used an array instead of a template there where no problem. How
do I initialise the template vector so each particle has it''s own
template vector ??

Here are some snippets of code, which might be more understandable
than my rambling:

typedef TNT::Array1D<double> dVector3;
//typedef double dVector3[3];
enum orient {eHoris, eVertic};

//-----------------------------
typedef struct sParticle{
dVector3 position; //position of particle
sParticle():position(3)
{
}
}

list<sParticle> particlePool;
particlePool.resize(noParticles);
for(k=0;k<emitNoParticles;k++){
(*emit_iterator).position[0] = k++
}

this works when i use my array vector, but when using the Array1D
vector the position vector points to the same address for all
particles.

How do I ensure that each particle has its own vector ?




以下列出了未定义的内容在您发布的代码中。


TNT :: Array1D< double>

noParticles

emitNoParticles

emit_iterator


我无法回答这个问题,因为这些事情是未定义的,因此

我无法理解你的代码。


请阅读本文,这些是有关如何发布代码的一些指导原则。按照

这些指导原则,您将很快得到答案。

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8


john



Here are a list of things that are undefined in the code you posted.

TNT::Array1D<double>
noParticles
emitNoParticles
emit_iterator

I cannot answer this question because these things are undefined, therefore
I cannot understand your code any better than you can.

Please read this, these are some guidelines about how to post code. Follow
these guidelines and you will get your question answered quickly.

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

john




" John Harrison" <乔************* @ hotmail.com>在消息中写道

news:c4 ************* @ ID-196037.news.uni-berlin.de ...

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c4*************@ID-196037.news.uni-berlin.de...

丹尼斯 < DL ******* @ control.auc.dk>在消息中写道
新闻:ud *********** @ control.auc.dk ...

"Dennis" <dl*******@control.auc.dk> wrote in message
news:ud***********@control.auc.dk...


我有一个如上所述的小问题(在www上没有找到任何解决方案)。我有一个对象列表(粒子),我有一个模板向量的成员(来自TNT的array1d)。我在构造函数中初始化了这些成员,但是当我将不同的向量值写入不同的粒子时,值变得相同。

我发现每个粒子向量都指向到同一个地址:o(
Hi

I have a little problem as stated above (haven''t found any solution on
the www). I have a list of objects (particles) where I have a member
of a template vector (array1d from TNT). I initialise these members in
the constructor, but when I write different vector values to different
particles the values becomes the same.

I have found out that each particles vector are pointing to the same
address :o(




第二个想法也许答案是TNT :: Array1D是垃圾。


为什么不使用* standard * vector class std :: vector而不是使用可疑的

第三方的东西。


typedef std :: vector< double> dVector3;


我可以保证std :: vector不会与另一个

std :: vector共享内存。使用标准课程的好处是你可以在这里发布代码
,每个人都会知道你在用什么。


john



On second thoughts maybe the answer is that TNT::Array1D is rubbish.

Why not use the *standard* vector class std::vector rather than dubious
third party stuff.

typedef std::vector<double> dVector3;

I can promise you that a std::vector does not share memory with another
std::vector. Another advantage of using standard classes is that you can
post code here and everyone will know what you are using.

john


这篇关于带有模板成员共享地址的对象列表,为什么???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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