本文介绍了我可以重载运算符<<使用模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一些类有writeTo()的类。功能 但没有运算符<<。我想修复它以便它们 都有一个运算符<< (为了保持一致性)。 我可以这样做: 模板< class T> DataDest&安培; operator<<(DataDest& d,const T& t) { t.writeTo(d); 返回d; } 我尝试了以上但我收到的错误如没有运营商 发现哪个采取右手操作数类型XXX"。 我是否需要编写所有运算符<<和运算符>> 手工函数? 另外,为什么编译器不能编写运算符== 和运算符<对我来说自动?我有一些结构 ,其中约有50个成员(主要是浮动值)和 来编写/维护operator == / operator<这些函数对于它们是一种痛苦(更不用说容易出错了)。 我确定编译器可以自动为我做这个/> 与生成operator =和 复制构造函数的方式完全相同。 有没有一个很好的理由为什么这不是''干嘛?东西 我很想念......? - < \ ___ /> / OO \ \ _____ / FTB。对于电子邮件,请删除我的袜子。 我们判断候选人将如何通过他的员工创建广告系列广告来处理核危机。 这是一个完全没有意义的过程。I have some classes which have a "writeTo()" functionbut no operator<<. I want to fix it so that theyall have an operator<< (for consistency).Can I do something like this:template <class T>DataDest& operator<<(DataDest& d, const T& t){t.writeTo(d);return d;}I tried the above but I get errors like "no operatorfound which takes a right-hand operand of type XXX".Do I need to write all the operator<< and operator>>functions by hand?Also, a peeve: Why can''t the compiler write operator==and operator< for me automatically? I have some structswith about 50 members in them (mostly float values) andto write/maintain operator==/operator< functions forthem is a pain (not to mention bug-prone).I''m sure the compiler could do this for me automaticallyin exactly the same way that it generates operator= andcopy constructors.Is there a good reason why this wasn''t done? SomethingI''m missing...?--<\___/>/ O O \\_____/ FTB. For email, remove my socks.We?re judging how a candidate will handle a nuclearcrisis by how well his staff creates campaign ads.It?s a completely nonsensical process.推荐答案 它应该与模板配合使用: #include< iostream> #include< string> A级 { public: A(int data1 = 0,int data2 = 0) :data1_(data1), data2_(data2) { } void writeTo(std :: ostream& os)const { os<< ''[''<< data1_<< '';''<< data2_<< '']''; } 私人: int data1_; int data2_ ; }; 模板< typename T,typename S> S&运算符<<(S& d,const T& t) { t.writeTo(d); 返回d; } int main(int argc,char ** argv) { A a1 (3,5); A a2(-1,12); std :: string some_text("< - "); std :: cout<< a1<< some_text<< a2<< ''\\ n''; }It should work with template:#include <iostream>#include <string>class A{public:A(int data1 = 0, int data2 = 0): data1_(data1),data2_(data2){}void writeTo(std::ostream& os) const{os << ''['' << data1_ << '';'' << data2_ << '']'';}private:int data1_;int data2_;};template<typename T, typename S>S& operator<<(S& d, const T& t){t.writeTo(d);return d;}int main(int argc,char **argv){A a1(3, 5);A a2(-1, 12);std::string some_text(" <--");std::cout << a1 << some_text << a2 << ''\n'';} 好吧,它可以做到,但事实并非如此。它是按照这种方式设计的,所以请按原样使用 。Well, it could do it, but it does not. it is designed this way, so takeit as it is. 这应该有效。你的代码有点短,所以很难说为什么它不会是b $ b。最好显示一个最小但完整的程序,它可以显示观察到的行为。以下程序编译没有错误 这里: class DataDest { }; class MyClass { public: void writeTo(DataDest& d)const { } }; 模板< class T> DataDest& operator<<(DataDest& d,const T& t) { t.writeTo(d); 返回d; } int main() { MyClass x; DataDest d; d<< x; }That should work. Your code is a bit short, so it''s hard to tell why itwouldn''t. It''s better to show a minimal, but complete program that exhibitsthe observed behavior. The following program does compile without errorhere:class DataDest{};class MyClass{public:void writeTo(DataDest& d) const {}};template <class T>DataDest& operator<<(DataDest& d, const T& t){t.writeTo(d);return d;}int main(){MyClass x;DataDest d;d << x;} 模板应该没问题。The template should be fine. 我认为这对comp.std.c ++来说更像一个问题。那个新闻组用什么比如何处理更多。在这里,我们只接受C ++语言,因为它是。 ;-)I think that''s more of a question for comp.std.c++. That newsgroup dealsmore with the why than the how. Here, we just accept the C++ language as itis. ;-) 是的,这是大型原始痴迷对象的常见问题。 你需要在这里重新考虑你的设计并更好地分组你的数据。 如果你想要一种会让你的设计错误变得更容易的语言 与你合作你来到了错误的地方。幸运的是,这个不太难以解决,但如果你有很多地方可以获得这些数据 直接访问你将会在那里一段时间。Yep, that is a common problem with large, primitive obsessed objects.You need to rethink your design here and group your data better.If you want a language that will make your design mistakes easier towork with you''ve come to the wrong place. Luckily this one isn''t toohard to fix though if you have a bunch of places where this data isaccessed directly you''re going to be there for a while. 这篇关于我可以重载运算符<<使用模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-06 20:43