[编辑] 我正在写一个模板类与类型参数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;
}
请**用[mcve]或[SSCCE(Short,Self Contained,Correct Example)](http://sscce.org)您的问题 – NathanOliver