【问题标题】:Operator<< overload for vector of pointers throwing error指针向量的运算符<<重载抛出错误
【发布时间】:2015-09-01 00:29:43
【问题描述】:

我有这个程序,它只是 C++ 的复习,我不断地通过重载运算符

驱动程序.cpp

#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include "ToolB.h"
#include "Rock.h"
#include "Scissors.h"
#include "Paper.h"
using namespace std;

const int SIZE = 10;

int main()
{

  srand(time(NULL));

  vector<ToolB *> army;
  int strRand, typeRand;

  for (int i = 0; i < SIZE; i++)
  {
    typeRand = rand() % 3;
    strRand = rand() % 11;

    if (typeRand == 0)
      army.push_back(new Rock(strRand));
    else if (typeRand == 1)
      army.push_back(new Paper(strRand));
    else
      army.push_back(new Scissors(strRand));
  }

  ToolB::displayToolBs(army, SIZE);
  cout << endl;

  return 0;

}

ToolB.h/ToolB.cpp

#ifndef TOOLB_H_
#define TOOLB_H_

#include <vector>
using namespace std;

class ToolB
{

 public:
  ToolB();
  void setStrength(int s);
  char getType() const;
  int getStrength() const;
  static void displayToolBs(vector<ToolB *> &v, const int &size);

 protected:
  char m_type;
  int m_str;

};

#endif

//////////////////////////////////////////////////////////////////

#include <iostream>
#include "ToolB.h"
using namespace std;

ToolB::ToolB()
{
  m_str = -1;
}

void ToolB::setStrength(int s)
{
  m_str = s;
}

int ToolB::getStrength() const
{
  return m_str;
}

char ToolB::getType() const
{
  return m_type;
}

void ToolB::displayToolBs(vector<ToolB *> &v, const int &size)
{
  for (int i = 0; i < size; i++)
    cout << *v[i];
}

Rock.h/Rock.cpp

#ifndef ROCK_H_
#define ROCK_H_

#include "ToolB.h"
using namespace std;

class Rock : public ToolB
{

 public:
  Rock(int s);
  bool fight(ToolB t);
  friend ostream& operator<<(ostream& os, const Rock &r);

};

#endif

//////////////////////////////////////////////////////////

#include <iostream>
#include "Rock.h"
using namespace std;

Rock::Rock(int s) : ToolB()
{
  m_str = s;
  m_type = 'r';
}

bool Rock::fight(ToolB t)
{
  int newStr;

  if (t.getType() == 's')
    newStr = m_str * 2;
  else if (t.getType() == 'p')
    newStr = m_str / 2;
  else
    newStr = m_str;

  if (newStr > t.getStrength())
    return true;
  else
    return false;
}

ostream& operator<<(ostream& os, const Rock &r)
{
  os << "Rock: " << r.getStrength() << endl;
  return os;
}

PaperScissors 类与 Rock 类完全相同,除了一些较小的值更改,因此我没有发布该代码。

在 Driver.cpp 中,ToolB 的静态方法 displayToolBs 应该为派生类的所有实例(PaperRockScissors)调用 coutvector&lt;ToolB *&gt; army 但是当我编译并运行程序时,我得到了这个输出:

ToolB.cpp: In static member function ‘static void ToolB::displayToolBs(std::vector<ToolB*, std::allocator<ToolB*> >&, const int&)’: ToolB.cpp:39: error: no match for ‘operator<<’ in ‘std::cout << *((std::vector<ToolB*, std::allocator<ToolB*> >*)v)->std::vector<_Tp, _Alloc>::operator[] [with _Tp = ToolB*, _Alloc = std::allocator<ToolB*>](((long unsigned int)i))’

我知道解决方法;但是,我的说明特别说明要在除 ToolB 之外的所有类中创建 cout 重载。

我几乎尝试了所有方法,但没有什么能提供我需要的输出。

谢谢!

【问题讨论】:

    标签: c++ pointers inheritance operator-overloading


    【解决方案1】:

    您打印的是指针而不是它们指向的对象。在将指针发送到cout 之前,您需要取消引用它们,方法是使用cout &lt;&lt; *v[i] 而不是cout &lt;&lt; v[i]

    void ToolB::displayToolBs(vector<ToolB *> &v, const int &size)
    {
      for (int i = 0; i < size; i++)
        cout << *v[i];
    }
    

    【讨论】:

    • 结果是:ToolB.cpp: In static member function ‘static void ToolB::displayToolBs(std::vector&lt;ToolB*, std::allocator&lt;ToolB*&gt; &gt;&amp;, const int&amp;)’: ToolB.cpp:39: error: no match for ‘operator&lt;&lt;’ in ‘std::cout &lt;&lt; *((std::vector&lt;ToolB*, std::allocator&lt;ToolB*&gt; &gt;*)v)-&gt;std::vector&lt;_Tp, _Alloc&gt;::operator[] [with _Tp = ToolB*, _Alloc = std::allocator&lt;ToolB*&gt;](((long unsigned int)i))’ 和一堆其他错误。
    • 那是因为您没有在 ToolB.h 中声明 operator&lt;&lt;(ostream&amp; os, const tool&amp; x) 函数。您当前的方法并不能真正按原样工作,因为您想以多态方式打印。
    • 这个小任务的说明说要在除 ToolB :/ 之外的所有类中创建友元运算符重载。这就是为什么我过得很艰难。
    • @m_callens 那么说明要么错误,要么被误解。朋友不能是虚拟的——你需要ToolB 中的虚拟成员函数,而不是重载的运算符。
    • @molbdnilo 是的,我就是这么想的。我确定说明书要求的是不可能的事情,我只是想确保我没有遗漏任何东西。
    猜你喜欢
    • 1970-01-01
    • 2018-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    • 2018-05-14
    • 1970-01-01
    相关资源
    最近更新 更多