Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Indirection requires pointer operand



  • Hi Guys,

    I need Help.
    I'm getting the error "indirection requires pointer operand (" QTextStream "invalid).Why?
    Thanks

    void menupainel::on_btnConfirmar_clicked()
    {
        Pessoa *p=new Pessoa();
    
    
        QDate Mydate =ui->dateNasc->date();
        QString date = Mydate.toString();
    
        QString nome = ui->txtNome->text();
        QString idade = ui->txtIdade->text();
        QString sexo = ui->comboSexo->currentText();
        QString rg = ui->txtRG->text();
        QString email = ui->txtEmail->text();
        QString cidade = ui->txtCity->text();
        QString estado = ui->txtEstado->text();
        QString telefone = ui->txtTelefone->text();
        QString celular = ui->txtCel->text();
    
        p->setNome(nome.toStdString());
        p->setIdade(idade.toStdString());
        p->setSexo(sexo.toStdString());
        p->setRG(rg.toStdString());
        p->setData(date.toStdString());
        p->setEstado(estado.toStdString());
        p->setTelefone(telefone.toStdString());
        p->setCelular(celular.toStdString());
        p->setEmail(email.toStdString());
    
    
    
        QFile file("text.txt");
    
            if(!file.open(QIODevice::Append|QIODevice::Text))
                return;
    
            QTextStream out(&file);
            *out<<p->getNome()<<endl;
    
    
        using namespace std;
    
        ui->stackedWidget->setCurrentIndex(2);
    }
    

  • Qt Champions 2019

    @Caio.Sousa said in Indirection requires pointer operand:

    *out<<p->getNome()<<endl;

    out is already an Object here, no need to dereference it again
    And it's also not needed to create Pessoa on the heap here. You even forgot to delete it and created a memory leak.



  • So would it look like? I'm new to C ++ and QT



    • *out<<p->getNome()<<endl; becomes out<<p->getNome()<<endl;
    • remove using namespace std;
    • replace Pessoa *p=new Pessoa(); with auto p = std::make_unique<Pessoa>(); (requires #include <memory>)


  • @VRonin said in Indirection requires pointer operand:

    make_unique

    Ok, I'll try, but removing the * out to out gave me a problem talking about the QString, when I put the QString variable it works normal


  • Lifetime Qt Champion

    @Caio.Sousa
    Hi
    Are you using std::string ?

    QTextStream out(&file);
    out<<p->getNome()<<endl;

    QTextStream dont like std::string as far as i know
    and it seems p->getNome() returns a std::string ?
    Any reason why its not QStrings in Pessoa ?



  • @mrjj

    I did this:

    Dou um QString nome = ui->txtNome->text();
    p.setNome(nome.toStdString());

    there in the file I try to use the out<<p.getNome();

    Only then the error appears = "invalid operands to binary expression('QTextStream' and 'std::........'"


  • Lifetime Qt Champion

    Hi
    I was wondering if you need
    std::string ?
    If you change to QString, it will just work.


Log in to reply