[英]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:我正在尝试模拟一些等离子体物理,为此我决定将我的“模拟世界”创建为 class,在“World.h”文件中定义:
#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: class 原型的实现如下:
#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:我遇到的问题是,当我从 main 调用 function "World::setTime(/**/)" 时:
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
[错误] 在没有 object 的情况下无法调用成员 function 'void World::setTime(double, int)'
Referring to the value of 'int num_ts' given as an argument.引用作为参数给出的 'int num_ts' 的值。 What is the prblem?
问题是什么? What is the object it is referring to?
它指的是什么object?
I was reading this post:我正在阅读这篇文章:
cannot call member function without object 不能调用没有 object 的成员 function
but I cannot apply the solution in there because I wrote down a constructor in my class.但我无法在其中应用解决方案,因为我在 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 .我认为问题在于您正在调用已定义 class的成员 function 而不是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 .这样,您将调用一个 object ,您已将其定义为World类型的“世界”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.