call to non-static member function without an object argument
时间: 2024-05-04 13:18:44 浏览: 35
这个错误通常发生在你试图调用一个非静态成员函数却没有提供对象参数的情况下。这意味着你需要先创建一个类的对象,然后通过该对象来调用该成员函数。
例如,如果你有一个类 `Foo`,其中有一个非静态成员函数 `bar()`,你需要先创建一个 `Foo` 对象,然后使用该对象来调用 `bar()`:
```
Foo myFoo;
myFoo.bar();
```
如果你尝试在没有创建 `Foo` 对象的情况下直接调用 `bar()`,编译器就会报出“call to non-static member function without an object argument”的错误。
相关问题
dialog.cpp:129:21: error: call to non-static member function without an object argument
这个错误通常是因为你尝试在一个非静态成员函数中调用另一个非静态成员函数时,没有给这个函数提供一个对象参数。这个对象参数指的是调用这个成员函数的对象,也就是说,你需要先创建这个对象,然后使用这个对象来调用这个成员函数。
例如,在下面的代码中:
```
class MyClass {
public:
void func1() {
// do something
}
void func2() {
// call func1 without an object argument
func1();
}
};
int main() {
MyClass obj;
obj.func2();
return 0;
}
```
在 `func2` 中调用 `func1` 时,没有给 `func1` 提供一个对象参数。解决方法是要么将 `func1` 声明为静态成员函数,要么在调用 `func1` 时提供一个对象参数。例如:
```
class MyClass {
public:
void func1() {
// do something
}
void func2() {
// call func1 with an object argument
this->func1();
}
};
int main() {
MyClass obj;
obj.func2();
return 0;
}
```
在 `func2` 中使用 `this->func1()` 来调用 `func1` 并提供一个对象参数。
widget.cpp:58:24: error: call to non-static member function without an object argument
这个错误通常发生在你试图调用一个非静态成员函数但没有提供对象参数的情况下。这通常是因为你尝试在类的定义中调用非静态成员函数,而非静态成员函数需要通过对象来调用。
你需要创建类的一个实例对象,然后使用该对象来调用非静态成员函数,例如:
```
MyClass myObject;
myObject.myMemberFunction(); // 调用非静态成员函数
```
如果你在类定义中调用非静态成员函数,那么编译器不知道应该使用哪个对象来调用该函数,因此会产生错误。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)