C++ 继承:基类中重载 operator+ 的 2 次在派生类中无法正常工作
c++ inheritance: 2 times overloaded operator+ in base class are not working proper in derived class
我基于 std::valarray 编写了我的泛型类 "MyVector">
//myvector.h
#ifndef MYVECTOR_H
#define MYVECTOR_H
#include <valarray>
template <typename T>
class MyVector
{
public:
MyVector(int size){larr.resize(size);}
MyVector<T>& operator=(const MyVector<T>& other)
{
this->larr=other.larr;
return *this;
}
MyVector<T> operator+ ( const MyVector<T>& rhs); //body in .cpp file
template <typename U>
MyVector<T> operator+ (const U& val); //body in .cpp file
protected:
std::valarray larr;
}
#endif
//myvector.cpp
/*****other code*****/
template <typename T>
MyVector<T> MyVector<T>::operator+ ( const MyVector<T>& rhs)
{
MyVector<T> lv; lv.larr = this->larr + rhs.larr;
return lv;
}
template <typename T>
template <typename U>
MyVector<T> MyVector<T>::operator+ (const U& val)
{
MyVector<T> lv; lv.larr = this->larr + static_cast<T> (val);
return lv;
}
/*****other code*****/
然后我尝试编写一个派生类 DataVector,其中包含 MyVector 的所有函数,尤其是我所有的重载运算符。
#ifndef DATAVECTOR_H
#define DATAVECTOR_H
#include "myvector.h"
class dataVector : public MyVector<int>
{
public:
dataVector& operator=(const dataVector& other)
{
this->larr=other.larr;
return *this;
}
using MyVector<int>::operator=;
using MyVector<int>::operator+;
}
#endif
当我尝试编译main.cpp时,
//main.cpp
#include "datavector.h"
#include "myvector.h"
dataVector datav1(10);
dataVector datav2(10);
dataVector datav3(10);
//if i write:
datav1=datav1 + 10; //works (gmake and compiler gcc7.5 on ubuntu)
//if i write:
datav3=datav1 + datav2; //does not work (gmake and compiler gcc7.5 on ubuntu)
我得到这个编译器错误:
myvector.cpp: In instantiation of ‘MyVector<T> MyVector<T>::operator+(const U&) [with U = dataVector; T = int]’:
myvector.cpp:xxx:yy: error: invalid static_cast from type ‘const dataVector’ to type ‘int’
MyVector<T> lv; lv.larr = this->larr + static_cast<T> (val);
如果我使用 MyVector:MyVector3=MyVector1+MyVector2 效果很好。
谁能帮我? 我知道这段代码写得不好,但我仍在学习。 谢谢。
问题是,当你将MyVector
添加到MyVector
时,MyVector::operator+ ( const MyVector<T>& rhs)
匹配,但是当你将dataVector
添加到dataVector
时,dataVector::operator+ (const U& val)
(继承自MyVector
(成为更好的匹配,因为它接受任何东西,不一定是T
隐蔽的东西。有几种可能的解决方案。
- 如果合适,
dataVector
像using dataVector = MyVector<int>
一样做一个 typedef。 - 添加一个接受
dataVector
operator+
;它可以使用显式强制转换(如*this + (const MyVector<int> &)rhs
(调用继承的运算符。 - 将
MyVector::operator+ (const U& val)
限制为仅接受T
,如 Jarod42 所建议的那样。 - (艰难的方法(限制
MyVector::operator+ (const U& val)
只接受使用SFINAE的理智类型(如template <typename U, typename = typename std::enable_if<std::is_arithmetic<U>::value>::type>
,或者is_convertible_to
,或任何合适的(。
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 为什么STD ::计数将常数传递给Lambda,而不是在弦上工作时而不是字符
- C++程序已停止工作-求解常微分方程