【问题标题】:KAuth API line of code is throwing an error telling me to convert const char* to non scalarKAuth API 代码行抛出一个错误,告诉我将 const char* 转换为非标量
【发布时间】:2012-08-18 22:50:26
【问题描述】:

我正在学习本教程:

bit.ly/SBMmwp

我在第一行收到以下错误:

错误:请求从“const char [26]”转换为非标量类型“KAuth::Action”

KAuth::Action readAction = "org.kde.auth.example.read";

KAuth::ActionReply reply = readAction.execute();
if (reply.failed())
{
    QMessageBox::information(this, "Error", QString("KAuth returned an error code: %1").arg(reply.errorCode()));
}
else
{
    QMessageBox::information(this, "Done!", QString("Successfully authenticated!"));
}

错误在第一行:KAuth::Action readAction = "org.kde.auth.example.read";

为什么会这样?这是与滥用 KAuth API 相关的一些错误,还是常见的 C/C++ 错误。我什至不知道如何将 const char[26] 转换为 non-scalar 类型。





@n.m 不,这是创建主窗口 (GUI) 的事件。完整的代码类似于:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <kauth.h>
#include <QMessageBox>
#include <kauthaction.h>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    KAuth::Action readAction = "org.kde.auth.example.read";
    KAuth::ActionReply reply = readAction.execute();
    if (reply.failed())
    {
        QMessageBox::information(this, "Error", QString("KAuth returned an error code: %1").arg(reply.errorCode()));
    }
    else
    {
        QMessageBox::information(this, "Done!", QString("Successfully authenticated!"));
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

【问题讨论】:

  • 这是您要编译的整个程序吗?

标签: c++ authentication


【解决方案1】:

我不熟悉 KDE,但http://www.purinchu.net/kdelibs-apidocs/kdecore/html/classKAuth_1_1Action.html 建议您不能将字符串分配给“Action”。假设可以从“const char*”创建“QString”(对于字符串类并不罕见),将行更改为

KAuth::Action readAction("org.kde.auth.example.read");

可能会修复它。

注意:教程不一定正确;他们的作者并不总是跟上变化。

【讨论】:

  • 刚试过,但它把错误改成了“未定义的对 KAuth::Action::Action(Qstring const&) 的引用”我想问题出在我下载的库上,但我没有不知道如何得到旧的。
【解决方案2】:

我基本上被打脸了。它就像 Christian 所写的那样完成,但是我得到未定义的引用等等等等的原因是因为我需要链接到 KDECore 库。如果您使用的是 QT,您将拥有一个 .pro 文件。你所要做的就是写在里面:

LIBS += -lkdecore

但如果您在项目中使用 CMake,则需要在 CMake.txt 中添加这些行:

find_package(KDE4 REQUIRED)
target_link_libraries([name_of_application] kdecore)

或者:

find_package(KDE4 REQUIRED)
target_link_libraries([name_of_application] ${KDE4_KDECORE_LIBS})

其中 [name_of_application] 是要构建的应用程序的名称(通常是项目名称)。要记住的一件事是,如果您使用 CMake,则必须根据应用程序的需要添加其他库。否则,您将获得对应用程序中其他组件的更多未定义引用。例如,如果我在我的应用程序中使用 QT GUI 组件,我会在 CMake.txt 中执行:

find_package(KDE4 REQUIRED)
target_link_libraries(My_Application_Name kdecore ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY})

【讨论】:

    猜你喜欢
    • 2020-01-23
    • 1970-01-01
    • 2015-07-09
    • 2014-01-09
    • 1970-01-01
    • 2021-09-16
    • 2014-12-20
    • 1970-01-01
    • 2022-01-10
    相关资源
    最近更新 更多