简体   繁体   English

QT:无法访问在类“ QByteArray”中声明的私有成员

[英]QT: cannot access private member declared in class 'QByteArray'

I've been trying to create a random phrase generator, which reads nouns from one text file and verbs from another text file. 我一直在尝试创建一个随机短语生成器,该生成器从一个文本文件读取名词,从另一个文本文件读取动词。 That all worked, but now I'm trying to write a method that capitalizes the first letter of the subject, but keep getting the errors 一切正常,但是现在我正在尝试编写一种将主题的第一个字母大写的方法,但是会不断出现错误

error: C2248: 'QByteArray::operator QNoImplicitBoolCast' : cannot access private member declared in class 'QByteArray' 错误:C2248:'QByteArray :: operator QNoImplicitBoolCast':无法访问在类'QByteArray'中声明的私有成员

see declaration of 'QByteArray::operator QNoImplicitBoolCast' 参见“ QByteArray :: operator QNoImplicitBoolCast”的声明

see declaration of 'QByteArray' 参见“ QByteArray”的声明

I'll post the code for the method (sorry if its not in proper format I'm new) 我将发布该方法的代码(很抱歉,如果它的格式不正确,我是新手)

    void MainWindow::returnCap(QString sub){

        char *str;
        QByteArray ba;
        ba = sub.toLatin1();
        str = ba.data();
        QString firstLetter;
        firstLetter = str[0];
        QString cappedFirstLetter;
        cappedFirstLetter = firstLetter.toUpper();
        char flc; //firstLetterChar
        flc = cappedFirstLetter.toLatin1();
        str[0] = flc;
    }

Thanks for any help! 谢谢你的帮助!

The problem is that you assigning a byte array to a single character. 问题是您将字节数组分配给单个字符。 However you need only one character from the byte array: 但是,您只需要字节数组中的一个字符即可:

char flc; //firstLetterChar
flc = cappedFirstLetter.toLatin1()[0];

UPDATE: 更新:

I would solve your problem in the following way: 我将通过以下方式解决您的问题:

QChar c1 = sub[0];
c1 = c1.toUpper();
sub.replace(0, 1, c1);

You call the member function toLatin1 , which returns a QByteArray . 您调用成员函数toLatin1 ,该函数返回QByteArray You then assign this QByteArray object to a char variable (not char* , just char ). 然后,您将此QByteArray对象分配给char变量(不是char* ,只是char )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM