qstring转unsigned char时遇到widget.cpp:274:60: error: reference to non-static member function must be called; did yo
时间: 2024-03-15 20:44:36 浏览: 27
u mean 'unsigned char* QByteArray::data() const'?
这个错误是因为你试图直接将一个成员函数 `data()` 赋值给一个指针变量,而不是调用该函数并将其返回值赋值给指针变量。
如果你想将 `QByteArray` 转换为 `unsigned char*`,可以使用 `QByteArray` 类的 `data()` 函数来获取 `unsigned char*` 类型的指针:
```
QByteArray qstr = "example";
unsigned char* data = reinterpret_cast<unsigned char*>(qstr.data());
```
需要注意的是,`data()` 函数返回的指针指向 `QByteArray` 内部的数据缓冲区,如果你对该指针进行修改,可能会导致 `QByteArray` 的数据被破坏。因此,在使用 `data()` 函数返回的指针时,一定要小心处理。
相关问题
/home/wuyan/test/parse_threads.cpp:37: error: invalid use of non-static member function 'void parse_threads::read(int, QString)' sP104_class.setCallback(read);
这是一个编译错误,错误代码在第37行,错误原因是`read`是一个非静态成员函数,不能直接作为回调函数使用。需要将它改为静态成员函数或者将对象的地址作为参数传递给回调函数。可以尝试将代码修改为以下形式:
```
sP104_class.setCallback(&parse_threads::read);
```
或者将`read`函数改为静态成员函数:
```
static void read(int fd, QString data);
```
mainwindow.cpp:43:1: warning: control reaches end of non-void function
根据你提供的代码,编译器发出了一个警告:“控制流程到达了非 void 函数的结尾”。这是因为你的 `int MainWindow::selectFile()` 函数声明的返回类型是 `int`,但是在函数的结尾处没有返回语句。
根据你的代码逻辑,如果文件路径为空,则会打印错误消息并返回 `false`。否则,在执行导入操作之后,没有返回任何值。
要解决这个警告,你可以在函数结尾处添加一个默认的返回语句,以便处理所有可能的情况。例如,你可以在函数的最后返回一个默认值 `false`,表示导入操作失败。
以下是修正后的代码:
```cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
QPushButton *selectButton = new QPushButton("选择文件", this);
connect(selectButton, &QPushButton::clicked, this, &MainWindow::selectFile);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(selectButton);
QWidget *centralWidget = new QWidget(this);
centralWidget->setLayout(layout);
setCentralWidget(centralWidget);
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
bool guideCadIntoData(QString dir); // 在 mainwindow.h 文件中进行函数声明
int MainWindow::selectFile()
{
QString dir = QFileDialog::getOpenFileName(this, "选择文件", "", "DWG Files (*.dwg)");
if (dir.isEmpty()) {
qDebug() << "error: File import failure!";
return false;
} else {
// 进行导入操作
// ...
return true;
}
return false; // 添加默认的返回语句,处理其他情况
}
```
通过在函数的结尾处添加了 `return false;`,以处理控制流程到达函数末尾的情况,解决了警告。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)