【问题标题】:c++ operator < overload with class and interfacec ++运算符<重载类和接口
【发布时间】:2020-07-22 08:24:59
【问题描述】:

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

使这一切变得不合适的方法是运算符

邮政编码类;

#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 方法,因为它在实现时返回值。

【问题讨论】:

  • 这个问题中显示的代码不符合stackoverflow.com对minimal reproducible example的要求,因此这里的任何人都不太可能最终确定问题,而最多只能猜测.这个问题必须是edited 以显示一个最小示例,不超过一两页代码(“最小”部分),任何人都可以剪切/粘贴、编译、运行和重现所描述的问题(“ reproducible”部分)完全如图所示(这包括任何辅助信息,例如程序的输入)。请参阅How to Ask 了解更多信息。
  • 不要在抽象基类中重载运算符。重载子类中的运算符。
  • PostCode 未在IPostCode 声明的范围内声明。这个错误应该在错误列表的开头报告。
  • @ThomasMatthews 我已将代码内容更新为我的完整代码,您能否提供一个书面示例。您的意思是重载 main.cpp 中的方法吗?
  • 在您的代码中,将IPostCode::operator&lt; 移动到您的子类,即继承自IPostCode 的类。或者将其完全从基类中删除。

标签: c++ class interface abstract-class operator-keyword


【解决方案1】:

这并不能解决所有编译器警告和错误;只有operator&lt;
从类IPostCode 中删除operator&lt; 声明:

class IPostCode
{

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

};

PostCode 类中删除override 关键字:

class PostCode : public IPostCode
{

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

您确实不想在基类中覆盖比较运算符或实现比较函数。当你有一个基类类型的指针时,你不知道它真正指向了什么样的子类。您可以比较两个不同的子类。

【讨论】:

  • 谢谢@Thomas Matthews,这已经解决了问题。赞成帮助我理解问题,
猜你喜欢
  • 1970-01-01
  • 2016-02-19
  • 2015-05-09
  • 2011-11-29
  • 1970-01-01
  • 1970-01-01
  • 2012-11-26
  • 1970-01-01
相关资源
最近更新 更多