This refers to instantiation and "uninstantiation".

I need to solve an ode system many times with a varying paramter p in an iterative loop.

It seems that I cannot use "out" and "ode" repetitively in the code below.
The code works when N=1.

Is there a way of "uninstantiating" and then "reinstantiating" "out" and "ode" so that the program works for N>1?

I have searched the web and C++ books without success.

I'd appreciate some advice.

Best wishes

Alan

Doub x1, x2, h, p;
    Int N;
   
    x1=0.0, x2=1.0;
    N=5;
    for (Int i=0;i<N;i++)
    
    {
    p=0.1*i;
    Output out(1000);
    rhs_van d(p);
    Odeint<StepperDopr5<rhs_van> > ode(ystart,x1,x2,atol,rtol,h1,hmin,out,d);
    ode.integrate();
   
   }

Its hard to say why that code isn't working, since we have no idea what Output and Ode are -- resumeably c++ classes. That would normally not be a problem.

I have simplified the problem. If in main I have

Output out(1000);
Output out1(1000);

it works.

If in main I have

Output out(1000);
Output out(1000);

it does not work. "Redeclaration of Output out".

The struct Output code is now below.

Alan

struct Output {
	Int kmax;
	Int nvar;
	Int nsave;
	bool dense;
	Int count;
	Doub x1,x2,xout,dxout;
	VecDoub xsave;
	MatDoub ysave;
	Output() : kmax(-1),dense(false),count(0) {}
	Output(const Int nsavee) : kmax(500),nsave(nsavee),count(0),xsave(kmax) {
		dense = nsave > 0 ? true : false;
	}
	void init(const Int neqn, const Doub xlo, const Doub xhi) {
		nvar=neqn;
		if (kmax == -1) return;
		ysave.resize(nvar,kmax);
		if (dense) {
			x1=xlo;
			x2=xhi;
			xout=x1;
			dxout=(x2-x1)/nsave;
		}
	}
	void resize() {
		Int kold=kmax;
		kmax *= 2;
		VecDoub tempvec(xsave);
		xsave.resize(kmax);
		for (Int k=0; k<kold; k++)
			xsave[k]=tempvec[k];
		MatDoub tempmat(ysave);
		ysave.resize(nvar,kmax);
		for (Int i=0; i<nvar; i++)
			for (Int k=0; k<kold; k++)
				ysave[i][k]=tempmat[i][k];
	}
	template <class Stepper>
	void save_dense(Stepper &s, const Doub xout, const Doub h) {
		if (count == kmax) resize();
		for (Int i=0;i<nvar;i++)
			ysave[i][count]=s.dense_out(i,xout,h);
		xsave[count++]=xout;
	}
	void save(const Doub x, VecDoub_I &y) {
		if (kmax <= 0) return;
		if (count == kmax) resize();
		for (Int i=0;i<nvar;i++)
			ysave[i][count]=y[i];
		xsave[count++]=x;
	}
	template <class Stepper>
	void out(const Int nstp,const Doub x,VecDoub_I &y,Stepper &s,const Doub h) {
		if (!dense)
			throw("dense output not set in Output!");
		if (nstp == -1) {
			save(x,y);
			xout += dxout;
		} else {
			while ((x-xout)*(x2-x1) > 0.0) {
				save_dense(s,xout,h);
				xout += dxout;
			}
		}
	}
};

If course you can't declare the same object more than once. In your initial post, when you said it doesn't work, what exactly do you mean by that? What makes you believe it doesn't work?

If course you can't declare the same object more than once. In your initial post, when you said it doesn't work, what exactly do you mean by that? What makes you believe it doesn't work?

For the new simplified version below, the compiler reported the error
"redeclaration of Output out".

In the original looping program the compilation worked, but the program stopped executing (I believe) when it reached the second "Output output".

My question is "How do I avoid this problem?" I need to "destroy" the instantiation somehow.

Alan

Output out(1000);
Output out1(1000);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.