C++ c++;操作员<;重载类和接口
我目前正在从事一个项目,该项目从.csv文件读取邮政编码,完成haversine计算,并根据返回的搜索半径将项目存储在列表中。postcode变量通过类定义,并使用接口在main.cpp中实现 使这一切变得不合适的方法是操作符<重载,以提供邮政编码的字母排序(用于在main.cpp中对列表进行排序) 邮政编码类别C++ c++;操作员<;重载类和接口,c++,class,interface,abstract-class,operator-keyword,C++,Class,Interface,Abstract Class,Operator Keyword,我目前正在从事一个项目,该项目从.csv文件读取邮政编码,完成haversine计算,并根据返回的搜索半径将项目存储在列表中。postcode变量通过类定义,并使用接口在main.cpp中实现 使这一切变得不合适的方法是操作符邮政编码=”; 此->晶格度=0.0f; 此->经度=0.0f; } int PostCode::getId() { 返回此->Id; } std::string PostCode::getPostcode() { 返回此->邮政编码; } std::string Post
#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”
#包括
类别邮政编码:公共IPostCode{
私人:
int-Id;
std::字符串邮政编码;
std::字符串firstTwoChars;
双格;
双经度;
双距离中心;
公众:
邮政编码();
int getId()覆盖;
std::string getPostcode()重写;
std::string getFirstTwoChars()重写;
双getLattude()覆盖;
双getLongitude()覆盖;
双GetDistanceFromCenter()覆盖;
布尔运算符ID=0;
此->邮政编码=”;
此->晶格度=0.0f;
此->经度=0.0f;
}
int PostCode::getId()
{
返回此->Id;
}
std::string PostCode::getPostcode()
{
返回此->邮政编码;
}
std::string PostCode::getFirstTwoChars()
{
firstTwoChars=邮政编码.substr(0,2);
返回此->前两个字符;
}
双重邮政编码::getLattitude()
{
返回此->属性;
}
双邮政编码::getLongitude()
{
返回此->经度;
}
双重邮政编码::getdistanceFromCentre()
{
返回此->距离中心;
}
无效邮政编码::setId(std::string newId)
{
这个->Id=std::stoi(newId);
}
无效邮政编码::设置邮政编码(标准::字符串新邮政编码)
{
此->邮政编码=新邮政编码;
}
无效邮政编码::setLattitude(标准::字符串newLattitude)
{
这->latitude=std::stod(newlatitude);
}
无效邮政编码::设置经度(标准::字符串新经度)
{
此->经度=标准::stod(新经度);
}
无效邮政编码::setdistanceFromCentre(双新DistanceFromCentre)
{
此->距离中心=新距离中心;
}
无效邮政编码::clearPostCode(){
该->Id=0;
此->邮政编码=”;
此->晶格度=0.0f;
此->经度=0.0f;
}
bool PostCode::operator这并不能解决所有编译器警告和错误;只有运算符此问题中显示的代码不符合stackoverflow.com对a的要求,因此,这里的任何人都不可能最终确定问题,最多只能猜测。必须对该问题进行编辑,以显示一个最小的示例,不超过一页或两页的代码(“最小”部分),任何人都可以完全按照所示剪切/粘贴、编译、运行和再现所描述的问题(“可再现”部分)(这包括任何辅助信息,如程序输入)。有关更多信息,请参阅。不要在抽象基类中重载运算符。重载子类中的运算符。PostCode
未在IPostCode
声明的范围内声明。此错误应在错误列表的开头报告。@ThomasMatthew如果我已将代码内容更新为完整代码,请提供一个书面示例。你的意思是重载main.cpp中的方法吗?在你的代码中,moveIPostCode::operatorhank you@Thomas Matthews,这就解决了问题。投票赞成帮助我理解这个问题,
#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)
class IPostCode
{
public:
//--------------------------------------------------------
// Remove the function below
//--------------------------------------------------------
virtual bool operator<(const PostCode& right) const = 0;
};
class PostCode : public IPostCode
{
public:
//---------------------------------------------------
// Remove "override" from the declaration below.
//---------------------------------------------------
bool operator<(const PostCode& right) const override;
};