Error C2440 '<function-style-cast>': cannot convert from 'initializer list'
to 'Interval<int>'
no matching constructor for initialization of 'Interval<int>'
return Interval<T>(i1,i1);
您没有使用两个Interval<T>
和i1
的构造函数,而是Interval<T>
的。您必须使用两个T
(在这种情况下为int
:s)来构造它。
建议:&&
通常用于布尔运算,其结果为true
或false
。如果您想返回a ∩ b
(交叉点),我认为T T::operator&(const T2 &b) const;
会更合适。注意const
:s!您不应该更改任何值,而是返回一个新构造的Interval<T>
。
此外,if(&it)
不是必需的。 it
是const Interval<T>&
(引用)-引用必须始终引用某些内容,因此,&it
无法返回nullptr
。另一方面,如果it
可能是const Interval<T>*
(指针),则可能是nullptr
,然后检查就有意义了。这样引用很不错!
另一个注意事项:您不必在类定义中的<T>
之后使用Interval
。默认情况下,Interval
表示Interval<T>
。仅当您想将其与其他Interval
类型混合使用时,才需要为其提供类型。像这样:
template<typename T>
class Interval {
template<typename U>
void something(Interval& a,Interval<U>& b) {
// a is an Interval<T>&
// b is an Interval<U>&
}
};
您还不需要使流运算符friend
成为流运算符,因为它们不直接访问private
成员。为此,您拥有print()
和enter()
成员函数。
下面是代码中带注释的更多详细信息和建议:
#include <algorithm> // std::min,std::max
#include <iostream>
// half-open interval: [a,b)
template<typename T>
class Interval
{
public:
// default constructs an empty interval
Interval() :
Interval({},{}) // delegating to the below constructor
{}
Interval(const T& t1,const T& t2) : // <- use the member initializer list
a{t1},b{t2},flag{0}
{
if(b < a) b = a; // illegal,make it empty
if(a == b) std::cout << "EMPTY\n";
}
// itersection
Interval operator&(const Interval& it) const {
// Construct the Interval<T> using copy-list-initialization.
// A return statement with braced-init-list used as the return expression
// and list-initialization initializes the returned object.
return {
std::max(a,it.a),// get the greatest lower bound
std::min(b,it.b) // get the smallest upper bound
};
}
std::ostream& print(std::ostream& os) const {
return os << '[' << a << ',' << b << ')';
}
std::istream& enter(std::istream& is) {
return is >> a >> b;
}
private:
T a;
T b;
int flag;
};
// streaming operators do not need to be friends since they use public member functions
template<typename T>
std::ostream& operator<<(std::ostream& output,const Interval<T>& it) {
return it.print(output);
}
template<typename T>
std::istream& operator>>(std::istream& input,Interval<T>& it) {
return it.enter(input);
}
int main() {
Interval<int> x(0,10);
Interval<int> y(5,15);
Interval<int> u; // using the new default constructor
u = x & y;
std::cout << u << '\n'; // [5,10)
std::cout << (Interval<int>(0,20) & Interval<int>(5,15)) << '\n'; // [5,15)
std::cout << (Interval<int>(0,10) & Interval<int>(10,20)) << '\n'; // EMPTY [10,10)
std::cout << (Interval<int>(100,200) & Interval<int>(0,100)) << '\n'; // EMPTY [100,100)
}
,
您的代码中有很多错误。
我猜你在班级上方有一个template <typename T>
,或者整个代码是错误的
最重要的一个是在操作员&&
内
您仅声明了一个带有2个参数的构造函数,两个参数均为const T&
。
但是在您的成员函数中,您可以将构造函数调用与
混合在一起
const T&,const T&
const Interval<T>&,const Interval<T>&
const T&,const Interval<T>&
const Interval<T>&,const T&
其中一些调用将失败,因为您仅提供了以下构造函数。
Interval();
Interval(const T &,const T &);
// and copy/move constructors,if possible
您的流运算符也有问题,您使用流,但是您忽略了它,使用const引用接受了参数,但是您检查它是否不是nullptr
,引用不能被“制造”来自nullptr
的行为,这是未定义的行为。
请注意,尝试使用不同的编译器编写代码,clang发出以下警告,它更具可读性。
<source>:31:16: error: no matching constructor for initialization of 'Interval<int>'
return Interval<T>(i1,i1);
^ ~~~~~~
<source>:65:7: note: in instantiation of member function 'Interval<int>::operator&&' requested here
x && y;
本文链接:https://www.f2er.com/2776926.html