c++ - 'Invalid use of void expression' passing arguments of functions -
this piece of code solves systems of differential equations.
vector dydx(neq); void diffeq(vector x, vector &dydx) { dydx(0) = x(1); dydx(1) = -x(0); } double midpoint(int n, vector x) { double h=h/n; matrix z(n+1,n+1); z.fillrow(0,x); diffeq(x, dydx); z.fillrow(1,addvec(x, dydx*h)); //error: invalid use of void expression (int j=1; j<n; j++) { diffeq(z.getrow(j), dydx); z.fillrow(j+1, addvec(z.getrow(j-1), dydx*h*2)); //error: void value not ignored ought } diffeq(z.getrow(n), dydx); return 0.5*addvec(z.getrow(n), z.getrow(n-1), dydx*h); //error: invalid use of void expression }
the classes vector , matrix custom. vector
class vector { public: vector(size_t size): vsize(size), vdata(size){} int getsize(){return vsize;} double& operator()(size_t i){return vdata[i];} double operator()(size_t i) const {return vdata[i];} void operator+(double d) // these overload vector + int operator { (int i=0; i<vsize; i++) {vdata[i]=vdata[i]+d;} } void operator*(double d) { (int i=0; i<vsize; i++) {vdata[i]=vdata[i]*d;} } size_t vsize; vector<double> vdata; };
with function
vector addvec(vector v1, vector v2) { vector totvec(v1.getsize()); (int i=0; i<v1.getsize(); i++) {totvec(i) = v1(i) + v2(i);} return totvec; }
and same function 3 vectors.
now realise error means i'm passing void function, cannot figure out things go wrong. when try writing test program, seems work fine.
your operators return void
void operator+(double d) // these overload vector + int { (int i=0; i<vsize; i++) {vdata[i]=vdata[i]+d;} } void operator*(double d) { (int i=0; i<vsize; i++) {vdata[i]=vdata[i]*d;} }
so when call addvec(x, dydx*h)
calls addvec(x, void)
Comments
Post a Comment