C++ C++;固有性:基类型的函数签名不与派生类型一起使用
我有以下代码:C++ C++;固有性:基类型的函数签名不与派生类型一起使用,c++,inheritance,function-signature,C++,Inheritance,Function Signature,我有以下代码: class STFDataPoint { public: virtual ImagePoint get_patch_top_left() const = 0; virtual ImagePoint get_patch_bottom_right() const = 0; virtual std::string get_image_filename() const = 0; virtual ~STFDataPoint() = 0; }; inlin
class STFDataPoint {
public:
virtual ImagePoint get_patch_top_left() const = 0;
virtual ImagePoint get_patch_bottom_right() const = 0;
virtual std::string get_image_filename() const = 0;
virtual ~STFDataPoint() = 0;
};
inline STFDataPoint::~STFDataPoint() {}
class TrainingDataPoint : public STFDataPoint{
private:
int row;
int col;
std::string class_label;
ImagePoint patch_top_left;
ImagePoint patch_bottom_right;
std::string image_filename;
public:
TrainingDataPoint(int row, int col, std::string class_label,
const ImagePoint & top_left,
const ImagePoint & bottom_right,
std::string image_filename);
std::string get_class_label() const;
inline bool operator==(const TrainingDataPoint& other) const{
return other.class_label == this->class_label;
}
inline bool operator!=(const TrainingDataPoint& other) const{
return !(*this == other);
}
virtual ImagePoint get_patch_top_left() const;
virtual ImagePoint get_patch_bottom_right() const;
virtual std::string get_image_filename() const;
};
我正在尝试运行以下程序:
bool do_something(vector<STFDataPoint>& data_point){
return true;
}
int main(int argc, char* argv[]) {
ImagePoint left = ImagePoint(2,3);
ImagePoint right = ImagePoint(2,3);
TrainingDataPoint a = TrainingDataPoint(1,2,"",left, right, "");
vector<TrainingDataPoint> b;
b.push_back(a);
do_something(b);
}
bool do\u something(向量和数据点){
返回true;
}
int main(int argc,char*argv[]){
ImagePoint left=ImagePoint(2,3);
ImagePoint right=ImagePoint(2,3);
TrainingDataPoint a=TrainingDataPoint(1,2,“”,左,右,”);
载体b;
b、 推回(a);
做某事(b);
}
但会出现以下错误:
invalid initialization of reference of type ‘std::vector<STFDataPoint>&’ from expression of type `std::vector<TrainingDataPoint>`
从'std::vector'类型的表达式初始化'std::vector&'类型的引用无效`
但是,如果我将do_something()
的签名更改为接受STFDataPoint
(不是它们的向量),则运行正常。有人能解释一下为什么会这样,以及是否有解决办法
谢谢类型向量
与向量
不同,两者之间没有转换<代码>向量不是向量
的基类型,即使a
是B
的基
可行的方法是使用指向基类型的指针或智能指针容器,并将函数更改为使用该容器:
bool do_something(vector<std::unique_ptr<STFDataPoint>>& data_point){
return true;
}
std::vector<std::unique_ptr<STFDataPoint>> b;
b.push_back( std::unique_ptr<STFDataPoint>(new TrainingDataPoint(1,2,"",left, right, "") ); // fill with any derived types of STFDataPoint
do_something(b);
bool do\u something(向量和数据点){
返回true;
}
std::载体b;
b、 向后推(std::unique_ptr(new TrainingDataPoint(1,2,“,left,right,”);//填充任何派生类型的STFDataPoint
做某事(b);
由于向量
不是向量
的子类型,因此无法执行此操作。向量不属于参数类型
但是,您可以使用模板执行某些操作使其正常工作:
template <typename T>
bool do_something(vector<T>& data_point){
//common actions like
ImagePoint leftPatch = data_point[0].get_patch_top_left();
return true;
}
模板
bool do\u something(向量和数据点){
//常见的行为如
ImagePoint leftPatch=data_point[0]。获取_patch_top_left();
返回true;
}
小心。std::unique_ptr(和shared_ptr)可能成为更好设计的拐杖。