简体   繁体   中英

Class function does not take arguments

I am trying to simulate some plasma physics and for that I decided to create my "Simulation world" as a class, defined in "World.h" file:

#ifndef _WORLD_H
#define _WORLD_H

class World{
    public:
        World(int _Nx, double _x0, double _xf); //Constructor prototype
        
        int _Nx; //Number of nodes
        double _dx; //Cell width
        
        void setTime(double _dt, int _num_ts);
        
    protected:
        double _x0; //System origin
        double _xf; //System ending
        
        double _dt = 0;     //time step length
        int _num_ts;        //number of time steps
};

#endif

The implementation of the class prototypes goes:

#include "World.h"

World::World(int Nx, double x0, double xf)
{
    this->_Nx = Nx;
    this->_x0 = x0;
    this->_xf = xf;
    this->_dx = (xf - x0)/(Nx - 1);
    
    //std::cout << Nx;
}

void World::setTime(double dt, int num_ts)
{
    this->_dt=dt;
    this->_num_ts=num_ts;
 }

The problem I am having is that when I call the function "World::setTime(/**/)" from main:

int main()
{
    //Create computational system
    World world(1000, 0.0, 0.1); //(Nx, x0, xm)
    
    World::setTime(world._dx, 10000);

    /*CODE*/

    return 0;
}

the compiler shows the message:

[Error] cannot call member function 'void World::setTime(double, int)' without object

Referring to the value of 'int num_ts' given as an argument. What is the prblem? What is the object it is referring to?

I was reading this post:

cannot call member function without object

but I cannot apply the solution in there because I wrote down a constructor in my class. Thank you for your replies!

I think that the problem is that you are calling member function of a defined class instead of an object . To fix that, I would try putting:

World world(1000,0.0,0.1); //(Nx,x0,xm)

world.setTime(world._dx, 10000);

This way you are calling an object that you have defined as " world " of type World .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM