0

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!

1 Answer 1

2

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];
}
Sign up to request clarification or add additional context in comments.

5 Comments

Which results with: 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))’ and a mess of other errors.
That's because you are not declaring an operator<<(ostream& os, const tool& x) function in ToolB.h. Your current approach isn't really going to work as-is since you want to print polymorphically.
The instructions for this small assignment say to create a friend operator overload in all classes EXCEPT ToolB :/. That's why Im having such a hard time.
@m_callens Then the instructions are either wrong or misunderstood. A friend can't be virtual — you need a virtual member function in ToolB, not an overloaded operator.
@molbdnilo Yes, that's what I figured. I'm certain that the instructions are asking for something impossible, I just wanted to make sure there was nothing I was missing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.