c ++运算符

问题描述 投票:-2回答:1

我目前正在从事一个项目,该项目从.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方法,因为它在隐含时会返回值。

c++ class interface abstract-class operator-keyword
1个回答
0
投票

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

class IPostCode
{

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

};

override类中删除PostCode关键字:

class PostCode : public IPostCode
{

public:
    //---------------------------------------------------
    //   Remove "override" from the declaration below.
    //---------------------------------------------------
    bool operator<(const PostCode& right) const override;
};
© www.soinside.com 2019 - 2024. All rights reserved.