报错must explicitly qualify name of member function when taking its address
时间: 2023-05-18 07:05:38 浏览: 961
这个错误通常是因为在尝试获取成员函数的地址时,没有显式地限定成员函数所属的类。
例如,假设有一个类A和一个成员函数foo:
```cpp
class A {
public:
void foo() {}
};
```
如果你尝试获取foo的地址,但没有限定它属于A类,就会出现这个错误:
```cpp
auto func_ptr = &foo; // 错误:必须显式限定成员函数所属的类
```
正确的写法是使用类名限定成员函数:
```cpp
auto func_ptr = &A::foo; // 正确:限定成员函数所属的类
```
这样就可以正确地获取成员函数的地址了。
相关问题
Constructor for 'Model' must explicitly initialize the member 'shader' which does not have a default constructor
The error message you are encountering suggests that there is a member variable called 'shader' in the 'Model' class that does not have a default constructor, and it needs to be explicitly initialized in the constructor of the 'Model' class.
To resolve this issue, you should add an explicit initialization for the 'shader' member variable in the constructor of the 'Model' class. You can do this by using an initializer list.
For example:
```cpp
class Model {
private:
Shader shader; // Assuming 'Shader' is the type of the 'shader' member variable
public:
Model() : shader(/* arguments for shader's constructor */) {
// Constructor code
}
};
```
Replace `Shader` with the actual type of the 'shader' member variable, and provide the necessary arguments for the 'shader' constructor within the initializer list.
By explicitly initializing the 'shader' member variable in the constructor, you ensure that it is properly initialized before any other code in the constructor executes.
constructor for 'MajorityChecker' must explicitly initialize the member 'tree' which does not have a default constructor
这个错误提示是因为在定义类`MajorityChecker`时,它有一个成员变量`tree`,但是这个成员变量没有默认构造函数,也就是说如果不显式地初始化它,就会出现这个错误。
解决这个问题的方法是在`MajorityChecker`的构造函数中显式地初始化`tree`。如果`tree`是一个指针类型,可以使用`new`运算符为它分配内存,并在构造函数中初始化它。例如:
```c++
class MajorityChecker {
public:
MajorityChecker(vector<int>& arr) {
// 为tree分配内存,并将其初始化为NULL
tree = new SegmentTree(0, arr.size()-1, arr);
}
// ...
private:
SegmentTree* tree; // tree是一个指向SegmentTree的指针类型的成员变量
};
```
如果`tree`是一个对象类型,则可以在构造函数的成员初始化列表中初始化它,例如:
```c++
class MajorityChecker {
public:
MajorityChecker(vector<int>& arr) : tree(0, arr.size()-1, arr) {
// ...
}
// ...
private:
SegmentTree tree; // tree是一个SegmentTree对象类型的成员变量
};
```
在上述代码中,`tree`的初始化使用了SegmentTree类的构造函数,并传递了参数0、arr.size()-1、arr,以便初始化tree成员变量。
阅读全文