Search code examples
c++classinterfaceabstract-classoperator-keyword

c++ operator < overload with class and interface


I am currently working on a project that reads postcodes from a .csv file, completes a haversine calculation and stores items in a list based on a returned search radius. The postcode variables are defined via a class and uses an interface to impliment in main.cpp

The method that has thrown this all out of place is the operator < overload to provide alphabetical sorting of the postcodes (to be used to sort the list in main.cpp)

Postcode class;

#ifndef POSTCODE_H_
#define POSTCODE_H_
#include <stdexcept>      // std::out_of_range
#include "IPostCode.h"
#include <string>


class PostCode : public IPostCode {


private:
    int Id;
    std::string Postcode;
    std::string firstTwoChars;
    double Lattitude;
    double Longitude;
    double distanceFromCentre;


public:

    PostCode();
    int getId() override;
    std::string getPostcode() override;
    std::string getFirstTwoChars() override;
    double getLattitude() override;
    double getLongitude() override;
    double getdistanceFromCentre() override;
    bool operator<(const PostCode& right) const override;

    void setId(std::string newId) override;
    void setPostcode(std::string newPostcode) override;

    void setLattitude(std::string newLattitude) override;
    void setLongitude(std::string newLongitude) override;
    void setdistanceFromCentre(double newdistanceFromCentre) override;  
    void clearPostCode() override;
};

PostCode::PostCode() {
    this->Id = 0;
    this->Postcode = "";
    this->Lattitude = 0.0f;
    this->Longitude = 0.0f;

}

int PostCode::getId()
{
    return this->Id;
}

std::string PostCode::getPostcode()
{
    return this->Postcode;
}

std::string PostCode::getFirstTwoChars()
{
    firstTwoChars = Postcode.substr(0, 2);
    return this->firstTwoChars;
}

double PostCode::getLattitude()
{
    return this->Lattitude;
}

double PostCode::getLongitude()
{
    return this->Longitude;
}

double PostCode::getdistanceFromCentre()
{
    return this->distanceFromCentre;
}

void PostCode::setId(std::string newId)
{
    this->Id = std::stoi(newId);
}

void PostCode::setPostcode(std::string newPostcode)
{
    this->Postcode = newPostcode;
}

void PostCode::setLattitude(std::string newLattitude)
{
    this->Lattitude = std::stod(newLattitude);
}

void PostCode::setLongitude(std::string newLongitude)
{
    this->Longitude = std::stod(newLongitude);
}

void PostCode::setdistanceFromCentre(double newdistanceFromCentre)
{
    this->distanceFromCentre = newdistanceFromCentre;
}

void PostCode::clearPostCode() {
    this->Id = 0;
    this->Postcode = "";
    this->Lattitude = 0.0f;
    this->Longitude = 0.0f;
}
bool PostCode::operator<(const PostCode& right) const
{
    return (Postcode.compare(right.Postcode) < 0);
}
#endif 

Interface code;

#ifndef IPOSTCODE_H_
#define IPOSTCODE_H_

#include <string>

class IPostCode {

public:
    virtual int getId() = 0;
    virtual std::string getPostcode() = 0;
    virtual double getLattitude() = 0;
    virtual double getLongitude() = 0;
    virtual double getdistanceFromCentre() = 0;
    virtual std::string getFirstTwoChars() = 0;
    virtual bool operator<(const PostCode& right) const = 0;

    virtual void setId(std::string newId) = 0;
    virtual void setPostcode(std::string newPostcode) = 0;
    virtual void setLattitude(std::string newLattitude) = 0;
    virtual void setLongitude(std::string newLongitude) = 0;    
    virtual void setdistanceFromCentre(double newdistanceFromCentre) = 0;

    virtual void clearPostCode() = 0;
};
#endif

Errors.

1. Error    C2259   'PostCode': cannot instantiate abstract class   (This error is for the main.cpp declaration of a PostCode)  
2. Error    C3668   'PostCode::operator <': method with override specifier 'override' did not override any base class methods    (Error within the postcode class)
3. Error    C4430   missing type specifier - int assumed. Note: C++ does not support default-int    
4. Error    C2143   syntax error: missing ',' before '&'    (3 + 4 = Errors within the interface)

I've read that the interface errors are due to type identifiers and i should declare them as static but this presents more error's. I am under the assumption that all methods within the interface are to be overridden as they declared pure virtual methods. (i.e = 0;). This isn't a void method because it returns values when implimented.


Solution

  • This does not resolve all the compiler warnings and errors; only the operator<.
    Remove the operator< declaration from class IPostCode:

    class IPostCode
    {
    
    public:
        //--------------------------------------------------------
        //  Remove the function below
        //--------------------------------------------------------
        virtual bool operator<(const PostCode& right) const = 0;
    
    };
    

    Remove the override keyword from the PostCode class:

    class PostCode : public IPostCode
    {
    
    public:
        //---------------------------------------------------
        //   Remove "override" from the declaration below.
        //---------------------------------------------------
        bool operator<(const PostCode& right) const override;
    };
    

    You really don't want to override comparison operators or implement comparison functions in base classes. When you have a pointer of the base class type, you don't know what kind of child class it really points to. You could be comparing two different child classes.