【发布时间】:2016-11-07 17:17:12
【问题描述】:
[编辑]
我正在编写一个带有类型参数T 的模板类。在该类的一个函数成员中,类型为T 的变量预计将写入std::ofstream 对象。在我使用自定义类型参数Fraction 实例化此类之前,一切都很好。尽管我确实重载了operator <<,但仍会发生错误。
// collection.h
#include <fstream>
template<typename T>
class Collection
{
public:
void writeToFile();
private:
T val;
};
template<typename T>
inline void Collection<T>::writeToFile()
{
std::ofstream file("output.txt");
file << val;
}
// Fraction.cpp
#include <iostream>
std::ostream& operator << (std::ostream& str, const Fraction& f)
{
std::cout << "Hello";
return str;
}
【问题讨论】: