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