c++ - 指针 vector 的运算符<<重载抛出错误

标签 c++ pointers inheritance operator-overloading

我有这个程序,它只是 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 , 应该调用 cout对于 Paper 中派生类的所有实例( RockScissorsvector<ToolB *> 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))’

我知道解决办法;但是,我的说明特别说明要创建一个 coutToolB 之外的所有类都重载.

我已经尝试了几乎所有的方法,但没有任何东西能给我所需的输出。

谢谢!

最佳答案

您正在打印指针而不是它们指向的对象。您需要在将指针发送到 cout 之前取消引用它们通过使用 cout << *v[i]而不是 cout << v[i] :

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

关于c++ - 指针 vector 的运算符<<重载抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32321838/

相关文章:

c++ - 命名空间到底是什么,为什么有必要

c++ - 为什么共享库和静态库不同?

c++ - 我如何使用 Qmake 获得定义的值?

c - 将二维动态数组的一维传递给函数

c - 指针操作

objective-c - objective-c 代码中的段错误

带有 lambda 比较器错误的 C++ priority_queue

c - 二叉搜索树删除中的指针问题

language-agnostic - 为什么箭头在继承中上升?

c# - 从C#中的继承类获取变量