我有一个模板方法,它重载了运算符<<。我需要在调用类的方法时调用该运算符。但是当我尝试它时会给我一些编译错误。
这是具体的Class Named BTreeLeave
的方法 bool BTreeLeave::burn() {
csv::WriterStream os("products.txt", std::ios_base::out);
os.set_delimiter(',');
if (os.is_open()) {
os << 1693 << NEWLINE;
os << 15 << 16 << 17 << 20 << NEWLINE;
os << "s" << "i" << "f" << NEWLINE;
os << 2 << NEWLINE;
os << "+" << NEWLINE;
os << "0" << NEWLINE;
}
模板功能就是这个
template<typename T>
typename csv::WriterStream& operator << (typename csv::WriterStream& ostm, const T& val)
{
if(!ostm.get_after_newline())
ostm.get_ofstream() << ostm.get_delimiter();
ostm.get_ofstream() << val;
ostm.set_after_newline(false);
return ostm;
}
template<>
inline csv::WriterStream& operator << (typename csv::WriterStream& ostm, const char& val)
{
if(val==NEWLINE)
{
ostm.get_ofstream() << NEWLINE;
ostm.set_after_newline(true);
}
else
ostm.get_ofstream() << val;
return ostm;
}
我得到的错误是 'csv :: WriterStream'不是从'std :: basic_ostream'派生的 和 &#34;'operator&lt;&lt;'不匹配(操作数类型是'csv :: WriterStream'和'int')&#34;
如果我尝试拨打&lt;&lt;主要功能的操作员是否工作。我做错了什么?
答案 0 :(得分:0)
你尝试下面的定义
template<typename T>
typename csv::WriterStream& operator << (typename csv::WriterStream& ostm, T val)
{
if(!ostm.get_after_newline())
ostm.get_ofstream() << ostm.get_delimiter();
ostm.get_ofstream() << val;
ostm.set_after_newline(false);
return ostm;
}