Pablo Estrada
Pablo Estrada

Reputation: 3452

QT C++ How to properly create functions with Object as Parameters

I created a class in C++ in which I have a method that receives as a parameter an object of the same class.

I get no errrors on my class definition, however when using the method I get the following error:

 error: C2662: 'bool Cliente::igual(const Cliente &)' : cannot convert 'this' pointer from 'const Cliente' to 'Cliente &'
Conversion loses qualifiers

I don´t need to modify the object I send as paramater so I think the best way to go is with references. However I´m pretty new to C++ and any suggestions on what to use (pointers, references, cons references ) will be appreciated.

Cliente.h

#ifndef CLIENTE_H
#define CLIENTE_H
#include "qstring.h"

class Cliente
{
public:
    Cliente();
    Cliente(QString,QString,QString);
    QString nombre;
    QString email;
    QString phone;
    bool igual( const Cliente& c);
    const QString getNombre();
    const QString getEmail();
    const QString getPhone();
};

#endif // CLIENTE_H

Cliente.cpp

#include "cliente.h"
#include "qstring.h"

Cliente::Cliente() {}

Cliente::Cliente(QString n, QString e, QString p){
    nombre = n;
    email = e;
    phone = p;
}

QString const Cliente::getNombre(){
    return nombre;
}
QString const Cliente::getEmail(){
    return email;
}
QString const Cliente::getPhone(){
    return phone;
}
bool Cliente::igual(const Cliente& c){
    Cliente::if(nombre == c.getNombre() && email == c.getEmail() && phone == c.getPhone()){
        return true;

    }
    return false;
}

Usage of method:

  QList<Cliente> clientList;
    while(query.next()){
        name = QString("A%1").arg(i);
        email = QString("B%1").arg(i);
        phone = QString("C%1").arg(i);
        QString valName = query.value(0).toString();
        QString valEmail = query.value(1).toString();
        QString valPhone = query.value(2).toString();
        Cliente cliente(valName,valEmail,valPhone);
        bool existe = false;
        for (int j = 0; j < clientList.size(); ++j) {
            if (clientList.at(j).igual(cliente)){
                existe = true;
                break;

            }
        }
        if(!existe){
            clientList.append(cliente);
            xlsx.write(name,valName);
            xlsx.write(email,valEmail);
            xlsx.write(phone,valPhone);
        }

        i++;
    }

The error happens at: clientList.at(j).igual(cliente))

Upvotes: 0

Views: 139

Answers (2)

  1. Your getter methods and the comparison should be const. You can also implement operator==, obviating the need for a method called igual.
  2. You're including the wrong files. The includes should read:

    #include <QString>
    

    not "qstring.h", even if it happens to work for the particular case of QString.

  3. Returning a const QString is pointless. The const there is redundant and has no effect.

  4. The data members that the getters are hiding should be made private, or at least protected.
  5. QString arguments should be passed as const references.
  6. Use initializer lists in constructors where appropriate.
  7. You can return the comparison result directly, after all it is a boolean.
  8. As a matter of style, the getters perhaps should be simply named foo instead of getFoo.

Taking the above into account, your code should look as follows:

Cliente.h

#ifndef PABLO_CLIENTE_H
#define PABLO_CLIENTE_H
#include <QString>

class Cliente
{   // The members below are all private.
    QString m_nombre;
    QString m_email;
    QString m_phone;
public:
    Cliente();
    Cliente(const QString &, const QString &, const QString &);
    bool operator==(const Cliente &) const;
    QString nombre() const;
    QString email() const;
    QString phone() const;
};
#endif

Cliente.cpp

#include "Cliente.h"

Cliente::Cliente() {}

Cliente::Cliente(const QString & nombre,
                 const QString & email, const QString & phone) :
  // Initializer list must match the order of member declarations!
  m_nombre(nombre), m_email(email), m_phone(phone)
{}

QString Cliente::nombre() const {
    return m_nombre;
}

QString Cliente::email() const {
    return m_email;
}

QString Cliente::getPhone() const {
    return m_phone;
}

bool Cliente::operator==(const Cliente & c) const {
  return nombre() == c.nombre() && email() == c.email() && phone() == c.phone();
}

For simple classes like this one, you could put all the code into the header:

Cliente.h (no .cpp!)

#ifndef PABLO_CLIENTE_H
#define PABLO_CLIENTE_H
#include <QString>

class Cliente
{   // The members below are all private.
    QString m_nombre;
    QString m_email;
    QString m_phone;
public:
    Cliente() {}
    Cliente(const QString & nombre,
            const QString & email, const QString & phone) :
      // Initializer list must match the order of member declarations!
      m_nombre(nombre), m_email(email), m_phone(phone)
    {}
    bool operator==(const Cliente & c) const {
      return nombre() == c.nombre() && email() == c.email() && phone() == c.phone();
    }
    QString nombre() const { return m_nombre; }
    QString email() const  { return m_email; }
    QString phone() const  { return m_phone; }
};
#endif

Upvotes: 1

Lahiru Chandima
Lahiru Chandima

Reputation: 24068

Add getters for nombre, email, and phone.

These getter methods should be const methods.

(eg: QString getNombre() const { return nombre; })

Then do the comparison like below

if(nombre == c.getNombre() && email == c.getEmail() && phone == c.getPhone()){
    return true;

}

Upvotes: 1

Related Questions