我目前正在进行一个项目,该项目从.csv文件中读取邮政编码,完成haversine计算,并根据返回的搜索半径将项目存储在列表中。邮政编码变量通过一个类定义,并使用接口在main.cpp 中实现
将这一切抛在一边的方法是运算符<重载以提供邮政编码的字母排序(用于对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方法,因为它在实现时返回值。
这并不能解决所有编译器警告和错误;只有CCD_ 1
从类IPostCode
:中删除operator<
声明
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;
};
您确实不想覆盖比较运算符或在基类中实现比较函数。当你有一个基类类型的指针时,你不知道它到底指向什么样的子类。