错误:与 'operator<<' 不匹配C++

发布于 2024-12-10 20:32:37 字数 1404 浏览 0 评论 0 原文

在我想问这个问题之前,我已经看过 stackoverflow 上的一些线程(因为已经有几个线程了),但即使通过实现一些解决方案,它似乎也无法帮助我解决问题。要么我做错了。

无论如何,我得到的错误是:

 error: no match for 'operator<<' in 'os << itr'

我的类是:

template <typename T> 
class btree {

  public:
    btree(size_t maxNodeElems);
    ~btree() {}

    struct node {   // <- this is just a declaration of a private inner-class
      list <T> elements;
      node *lvl;
    };

  private:
    size_t maxNodeElems;
    node*  root;   // <- this is the actual private member

};

template <typename T>
btree<T>::btree(size_t maxNodeElems) {
  if (maxNodeElems > 0) maxNodeElems = maxNodeElems;
  root = new node;
  root->lvl = new node[maxNodeElems+1];
}

template <typename T>
pair <typename btree<T>::iterator, bool> btree <T>::insert (const T& elem) {

  root->elements.push_back(elem);
  root->elements.sort();

  std::pair <typename btree<T>::iterator, bool> e;
  return e;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const btree<T>& tree) {

  class list <T>::iterator itr = tree.root->elements.begin();
  for (; itr != tree.root->elements.end(); ++itr) os << itr; 

  return os;
}

我很清楚我的插入没有返回任何内容,但我还没有完成该函数的实现,我只是想测试如何获取目前不在列表中的元素。有人可以告诉我我做错了什么吗?

I've looked at a few threads on stackoverflow before I thought of asking this question (since there has been a couple out there), but even through implementing some of the solutions, it doesn't seem to help my problem. That or I'm doing it wrong.

Anyways, as it goes, the error I'm getting is:

 error: no match for 'operator<<' in 'os << itr'

my class is:

template <typename T> 
class btree {

  public:
    btree(size_t maxNodeElems);
    ~btree() {}

    struct node {   // <- this is just a declaration of a private inner-class
      list <T> elements;
      node *lvl;
    };

  private:
    size_t maxNodeElems;
    node*  root;   // <- this is the actual private member

};

template <typename T>
btree<T>::btree(size_t maxNodeElems) {
  if (maxNodeElems > 0) maxNodeElems = maxNodeElems;
  root = new node;
  root->lvl = new node[maxNodeElems+1];
}

template <typename T>
pair <typename btree<T>::iterator, bool> btree <T>::insert (const T& elem) {

  root->elements.push_back(elem);
  root->elements.sort();

  std::pair <typename btree<T>::iterator, bool> e;
  return e;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const btree<T>& tree) {

  class list <T>::iterator itr = tree.root->elements.begin();
  for (; itr != tree.root->elements.end(); ++itr) os << itr; 

  return os;
}

I'm quite aware that my insert returns nothing, but I haven't finished implementing that function, I'm just trying to test how to get the elements out of the list at the moment. Can someone tell me what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥有 2024-12-17 20:32:37

使用 *itr 而不是 itr

os << *itr;

这是所需的行为。

现在,要使其正常工作,您必须确保类型 T 存在 operator<<,该类型基本上是 *itr 的类型。

除了手动循环之外,您还可以使用 std::copy :

template <typename T>
std::ostream& operator<<(std::ostream& os, const btree<T>& tree) {

 std::copy(tree.root->elements.begin(),
           tree.root->elements.end(),
           std::ostream_iterator<T>(os, " "));//print " " between 2 elements
  return os;
}

Use *itr instead of itr:

os << *itr;

This is the desired behaviour.

Now, for this to work, you have to make sure that operator<< exists for type T which is basically the type of *itr.

Instead of manual loop, you could use std::copy as well:

template <typename T>
std::ostream& operator<<(std::ostream& os, const btree<T>& tree) {

 std::copy(tree.root->elements.begin(),
           tree.root->elements.end(),
           std::ostream_iterator<T>(os, " "));//print " " between 2 elements
  return os;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文