C++ 尝试添加 >> 运算符重载到模板
C++ Trying to add >> Operator Overload To Template
我有一个 class IPrintable
是一个模板,还有一个 class 从它派生 - Date
。我想将运算符 >>
添加到模板,但一直收到错误消息:
source.cpp(16): error C2259: 'Date': cannot instantiate abstract class
source.cpp(16): note: due to following members:
source.cpp(16): note: 'void IPrintable<Date>::toIs(std::istream &)': is abstract
iprintable.h(19): note: see declaration of 'IPrintable<Date>::toIs'
我已经通过这种方式成功地将 <<
运算符添加到模板中:
virtual void toOs(ostream& os) const = 0;
friend ostream& operator << (ostream& output, const IPrintable& toPrint) {
toPrint.toOs(output);
return output;
}
然后我将 virtual void toOs(ostream& os) const = 0;
声明为 date.h
并在 date.cpp
中实现它。
这是我的IPrintable.h:
#pragma once
#include <iostream>
#include <string>
using namespace std;
template <class T>
class IPrintable {
private:
public:
virtual void toOs(ostream& os) const = 0;
friend ostream& operator << (ostream& output, const IPrintable& toPrint) {
toPrint.toOs(output);
return output;
}
virtual void toIs(istream& input) = 0;
friend istream& operator >> (istream& input, IPrintable& toSet) {
toSet.toIs(input);
return input;
}
};
这是在Date.h中的声明,在public
下:
virtual void toOs(ostream& output) const;
virtual void toIS(istream& input);
这是<<
(toOs
)和>>
(toIs
)在Date.cpp[=46中的实现=]:
void Date::toOs(ostream& output) const {
if (!isLeapYear(this->getDay(), this->getMonth(), this->getYear())) {
cout << "Not a leap year";
return;
}
output << getDay() << "/" << getMonth() << "/" << getYear();
}
void Date::toIS(istream& input) {
int index;
string str;
input >> str;
index = str.find('/');
this->setDay(stoi(str.substr(0, index)));
str = str.substr(index + 1);
index = str.find('/');
this->setMonth(stoi(str.substr(0, index)));
str = str.substr(index + 1);
this->setYear(stoi(str));
}
如果需要更多信息,请告诉我,我会尽力提供。
谢谢!
你的 Date::toIS
有大写字母 S,而你的 IPrintable::toIs
没有。如果将 override
添加到 Date::toIS
的声明中,您会发现它实际上并没有覆盖任何内容。
我有一个 class IPrintable
是一个模板,还有一个 class 从它派生 - Date
。我想将运算符 >>
添加到模板,但一直收到错误消息:
source.cpp(16): error C2259: 'Date': cannot instantiate abstract class
source.cpp(16): note: due to following members:
source.cpp(16): note: 'void IPrintable<Date>::toIs(std::istream &)': is abstract
iprintable.h(19): note: see declaration of 'IPrintable<Date>::toIs'
我已经通过这种方式成功地将 <<
运算符添加到模板中:
virtual void toOs(ostream& os) const = 0;
friend ostream& operator << (ostream& output, const IPrintable& toPrint) {
toPrint.toOs(output);
return output;
}
然后我将 virtual void toOs(ostream& os) const = 0;
声明为 date.h
并在 date.cpp
中实现它。
这是我的IPrintable.h:
#pragma once
#include <iostream>
#include <string>
using namespace std;
template <class T>
class IPrintable {
private:
public:
virtual void toOs(ostream& os) const = 0;
friend ostream& operator << (ostream& output, const IPrintable& toPrint) {
toPrint.toOs(output);
return output;
}
virtual void toIs(istream& input) = 0;
friend istream& operator >> (istream& input, IPrintable& toSet) {
toSet.toIs(input);
return input;
}
};
这是在Date.h中的声明,在public
下:
virtual void toOs(ostream& output) const;
virtual void toIS(istream& input);
这是<<
(toOs
)和>>
(toIs
)在Date.cpp[=46中的实现=]:
void Date::toOs(ostream& output) const {
if (!isLeapYear(this->getDay(), this->getMonth(), this->getYear())) {
cout << "Not a leap year";
return;
}
output << getDay() << "/" << getMonth() << "/" << getYear();
}
void Date::toIS(istream& input) {
int index;
string str;
input >> str;
index = str.find('/');
this->setDay(stoi(str.substr(0, index)));
str = str.substr(index + 1);
index = str.find('/');
this->setMonth(stoi(str.substr(0, index)));
str = str.substr(index + 1);
this->setYear(stoi(str));
}
如果需要更多信息,请告诉我,我会尽力提供。
谢谢!
你的 Date::toIS
有大写字母 S,而你的 IPrintable::toIs
没有。如果将 override
添加到 Date::toIS
的声明中,您会发现它实际上并没有覆盖任何内容。