[英]Assign value of variable in class C++
Hello I can't find out how to assign values to my variables that I am using in one of my class. 您好,我无法找到如何为我的课程之一中的变量分配值。 I am getting error nonstatic member reference must be relative to a specific object so I am doing something wrong but I cant figure how o make it right.
我收到错误消息, 非静态成员引用必须相对于特定对象,因此我做错了事,但我无法弄清楚该如何做对。
class Triedenie_cisla{
public:
Triedenie_cisla(int *poleHodnot, int ddlzka);
int *pole, dlzka;
double bubble_time, selection_time, insert_time, quick_time;
vector<int> mnozina_int;
string vypis_pola();
void BubbleSort_int();
void SelectionSort_int();
void InsertSort_int();
void QuickSort_int();
};
Then in functions that make sorts, I measure the time and trying to assign the time to variables like that 然后在进行排序的函数中,我测量时间并尝试将时间分配给类似的变量
Triedenie_cisla::insert_time = dif;
What I am doing wrong ? 我做错了什么? Thanks
谢谢
You need to find a good book on C++, those are basic things. 您需要找到一本有关C ++的好书,这些都是基础知识。
nonstatic member reference must be relative to a specific object
非静态成员引用必须相对于特定对象
means that to modify insert_time
you must do it on existing object of class: Triedenie_cisla
意味着要修改
insert_time
您必须在类的现有对象上进行修改: Triedenie_cisla
Then in functions that make sorts, I measure the time and trying to assign the time to variables like that
Triedenie_cisla::insert_time = dif;
然后,
Triedenie_cisla::insert_time = dif;
排序的函数中,我测量时间并尝试将时间分配给像Triedenie_cisla::insert_time = dif;
这样的变量Triedenie_cisla::insert_time = dif;
you should do it like that: 您应该这样做:
void Triedenie_cisla::BubbleSort_int() {
// ....
insert_time = dif;
//
}
You have to create an object of your class. 您必须创建您的类的对象。 Then, assigning values is possible.
这样就可以分配值。
Triedenie_cisla obj;
obj.insert_time = dif;
Otherwise, the compiler assumes that you want to change the value of a static variable, ie. 否则,编译器假定您要更改静态变量的值,即。 a variable that exists once for the whole class.
在整个类中只存在一次的变量。 To do so, you would have to state
为此,您必须声明
that insert_time is a static variable. 那insert_time是一个静态变量。 For example:
例如:
static double insert_time;
If I understand it well, you wish to set the value of insert_time inside your own sorting function which are already part of the class Triedenie_cisla. 如果我理解的很好,您希望在自己的排序函数(已经属于Triedenie_cisla类的一部分)中设置insert_time的值。
Therefore you just need to do 因此,您只需要做
this->insert_time = dif;
or even 甚至
insert_time = dif;
would be enough 够了
You can not call a variable/method only using class name unless it is static variable/method of the class. 除非它是类的静态变量/方法,否则不能仅使用类名称来调用变量/方法。 Therefore, solution of your problem is:
因此,您的问题的解决方案是:
Solution-1 解决方案1
First make a object of your class: 首先使您的类的对象:
Triedenie_cisla object_1 = new Triedenie_cisla() ;
Call the variable using object name. 使用对象名称调用变量。
object_1.insert_time = dif;
Solution-2 解决方案2
You can solve this problem also using static key word in front of insert_time in the class declaration: 您也可以在类声明中的insert_time前面使用静态关键字来解决此问题:
static double bubble_time, selection_time, insert_time, quick_time;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.