error: non-member function 'QString getName()' cannot have cv-qualifier QString getName() const; ^
时间: 2024-05-15 20:14:01 浏览: 8
这个错误提示意味着你定义的非成员函数 `getName()` 带有 `const` 修饰符,但是非成员函数不能有成员函数的 `const` 修饰符。如果你想让这个函数成为类的成员函数并且可以带有 `const` 修饰符,你需要将其定义为类的成员函数。
以下是一个示例:
```cpp
class MyClass {
public:
QString getName() const; // 成员函数可以带有 const 修饰符
};
QString MyClass::getName() const {
return name;
}
```
如果你不想将其定义为类的成员函数,你可以将 `const` 修饰符从函数声明和定义中移除。
```cpp
QString getName();
```
```cpp
QString getName() {
return name;
}
```
相关问题
error: no 'QString student::getName()' member function declared in class 'student' QString student::getName() ^
这个错误提示是说在类 `student` 中没有声明名为 `getName` 的成员函数,但是你在代码中调用了这个函数。
你需要在 `student` 类中声明这个函数,例如:
```cpp
class student {
public:
QString getName();
// 其他成员函数和成员变量
};
```
然后在类的实现文件中实现这个函数:
```cpp
QString student::getName() {
return name; // 返回名字
}
```
在调用 `getName()` 函数的地方,确保你是通过类的对象来调用这个函数。例如:
```cpp
student s;
QString name = s.getName();
```
form_para.cpp:336:30: Call to member function 'arg' is ambiguous qstring.h:313:31: candidate function qstring.h:315:31: candidate function qstring.h:1111:25: candidate function qstring.h:1113:25: candidate function qstring.h:1115:25: candidate function qstring.h:1117:25: candidate function qstring.h:1119:25: candidate function qstring.h:1121:25: candidate function
这个错误提示是因为在调用`arg()`函数时,编译器无法确定应该调用哪个函数。在QT中,`arg()`函数有多个重载形式,如果在调用时没有传递足够的参数信息,编译器就无法确定应该调用哪个函数。
为了解决这个问题,可以显式地指定要调用的`arg()`函数的重载形式,如下所示:
```cpp
int num = -255; // 负数
QString hex_str;
if (num >= 0) {
hex_str = QString("%1").arg(num, 8, 16, QChar('0'));
} else {
hex_str = QString("%1").arg(((1 << 32) - 1) ^ ((-num - 1) << 1) | 1, 8, 16, QChar('0'));
}
qDebug() << hex_str;
```
在调用`arg()`函数时,将要调用的重载形式显式地指定为`arg(int, int, int, QChar)`,这样编译器就能够确定要调用哪个函数了。
相关推荐
![md](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)
![](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)