Reputation: 3452
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
Reputation: 98425
const
. You can also implement operator==
, obviating the need for a method called igual
.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
.
Returning a const QString
is pointless. The const
there is redundant and has no effect.
QString
arguments should be passed as const references.foo
instead of getFoo
.Taking the above into account, your code should look as follows:
#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
#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:
#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
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