简体   繁体   中英

Operator<< overload for vector of pointers throwing error

I have this program that is just a refresher for C++ and I keep getting addresses to the pointers I'm trying to print via overloaded operator<<. Here is all the source code...

Driver.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;
}

The Paper and Scissors classes are the exact same as the Rock class except for some minor value changes so I didn't post that code.

In the Driver.cpp, the static method of ToolB's, displayToolBs , is supposed to call cout for all the instances of the derived classes ( Paper , Rock , and Scissors ) in the vector<ToolB *> army but when I compile and run the program, I get this output:

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))'

I know the fix to this; however, my instructions specifically say to create a cout overload in all classes EXCEPT ToolB .

I've tried just about everything, and nothing gives me the output I need.

Thanks!

You are printing the pointers rather than the objects they point to. You need to dereference the pointers before sending them to cout by using cout << *v[i] rather than cout << v[i] :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM