如何为操作员制作模板<<对于 ostream

编程入门 行业动态 更新时间:2024-10-10 23:23:53
本文介绍了如何为操作员制作模板<<对于 ostream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

以下内容不会为我编译.我没有想法……有什么帮助吗?

The following won't compile for me. I'm out of ideas... Any help?

template<>
inline
std::ostream& operator<< <const std::map<std::string, std::string> > (std::ostream& stream, const std::map<std::string, std::string>& some_map)
{
  return stream;
}

g++ 给了我以下错误:

g++ gives me the following error:

错误:'<' 之前的预期初始值设定项令牌

1好吧,既然每个人都告诉我要超载,那么让我举一个对超载没有意义的例子.如果我有这个怎么办:

1 Okay, since everyone is telling me to overload, let me give you an example that wouldn't make sense for overloading. What if I have this:

template <typename T>
inline
std::ostream& operator<<(std::ostream& stream, const T& something)
{
  stream << something.toString();
  return stream;
}

class Foo
{
public:
  Foo(std::string s)
  {
    name = s;
  }

  std::string toString() const 
  {
    return name;
  }

private:
  std::string name;
};

class Bar
{
public:
  Bar(int i)
  {
    val = i;
  }

  std::string toString() const 
  {
    std::ostringstream stream;
    stream << val;
    return stream.str();
  }

private:
  int val;
};

int main(int, char**)
{
  Foo foo("hey");
  Bar bar(2);

  std::cout << foo << std::endl;
  std::cout << bar << std::endl;
  return 0;
}

现在这也行不通.

我只是想避免重载运算符<<通过使用上面的模板一遍又一遍.这似乎应该是可能的.我想知道是否是,如果是,如何?

I just want to avoid having to overload operator<< over and over by using a template like above. This seems like it should be possible. I would like to know if it is, and if so, how?

在这种情况下,让 Foo 和 Bar 重载做同样的事情是一种浪费,这就是我试图避免它的原因.

In such a scenario, overloading for both Foo and Bar to do the same thing would be a waste, that is why I am trying to avoid it.

2好吧,看来我被误解了.这是另一种澄清的尝试:

2 Okay, it appears that I am being misunderstood. Here is another attempt to clarify:

template <typename T>
std::ostream& operator<<(ostream& stream, const T& t)
{
  for(typename T::const_iterator i = t.begin(), end = t.end(); i != end; ++i)
  {
    stream << *i;
  }
  return stream;
}

int main(int, char**) 
{
  set<int> foo;
  list<string> bar;
  vector<double> baz;

  cout << foo << " " bar << " " << baz << endl;
};

上面的代码不会让你介意.抱怨歧义.但它似乎是打印容器的更好解决方案.如果我用重载来做,我需要写一个 operator<< 的版本.对于每个容器/数据类型组合,这会产生大量的代码重复.

The above code won't work mind you. Complains about ambiguity. But it seems like the better solution for printing out containers. If I did it the with overloading, I would need to write a version of operator<< for each container/datatype combination, which would yield a ridiculous amount of code duplication.

推荐答案

这不需要是模板函数.

std::ostream & operator<<(std::ostream & stream, const std::map<std::string, std::string> & some_map)
{
    return stream;
}

编辑:

参考我关于用 C++ 编写 Java 的评论(对不起,如果这听起来很粗鲁,我不打算说脏话).告诉我这对你来说是否更好.而不是首先编写toString"方法,只需重载运算符<<;开始.功能几乎相同.然后,您可以编写一个非成员模板 toString 函数,该函数将自动与您的所有类一起工作,如下所示:

In reference to my comment about writing Java in C++(and sorry if it sounded rude, I didn't intend to be smarmy). Tell me if this doesn't work better for you. Instead of writing a "toString" method in the first place, just overload the operator<< to begin with. The function is nearly identical. Then, you can write a non-member template toString function that will automatically work with all of your classes, like this:

#include <sstream>
#include <string>

template<typename T>
std::string toString(const T & val)
{
    std::ostringstream ostr;
    ostr << val;
    return ostr.str();
}

编辑 2

如果您仍然坚持按自己的方式做,这是我的选择.使所有具有 toString 方法的类从具有虚拟 toString 方法的抽象类继承,然后编写一个运算符<<处理所有这些.

Here's my alternative if you still insist on doing it your way. Make all of your classes with the toString method inherit from an abstract class with a virtual toString method, then write one operator<< to handle all of them.

class Stringifiable
{
public:
    virtual std::string toString() const = 0;
};

std::ostream & operator<<(std::ostream & ostr, const Stringifiable& something)
{
    return ostr << something.toString();
}

现在编译器会选择你的重载而不是模板.

Now the compiler will choose your overload over templates.

这篇关于如何为操作员制作模板<<对于 ostream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 06:18:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405358.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:操作员   何为   模板   ostream   lt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!