Search code examples
c++qtcharqbytearraytoupper

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'

see declaration of 'QByteArray::operator QNoImplicitBoolCast'

see declaration of '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!


Solution

  • 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);