c++ operator < 重载 class 和接口

c++ operator < overload with class and interface

我目前正在从事一个项目,该项目从 .csv 文件中读取邮政编码,完成半正弦计算并根据返回的搜索半径将项目存储在列表中。邮政编码变量通过 class 定义,并使用接口在 main.cpp

中实现

让这一切变得不合时宜的方法是 operator < 重载以提供邮政编码的字母顺序排序(用于对 main.cpp 中的列表进行排序)

邮政编码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 

接口代码;

#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

错误。

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)

我读到接口错误是由于类型标识符引起的,我应该将它们声明为静态的,但这会带来更多错误。我假设接口中的所有方法都将被覆盖,因为它们声明了纯虚拟方法。 (即 = 0;)。这不是一个 void 方法,因为它 returns 在实现时的值。

这并不能解决所有的编译器警告和错误;只有 operator<.
从 class IPostCode 中删除 operator< 声明:

class IPostCode
{

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

};

PostCode class 中删除 override 关键字:

class PostCode : public IPostCode
{

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

您真的不想覆盖比较运算符或在基础 classes 中实现比较函数。当你有一个基本 class 类型的指针时,你不知道它真正指向哪种 child class。您可以比较两个不同的 child classes.