簡體   English   中英

運算符<<重載,指針向量拋出錯誤

[英]Operator<< overload for vector of pointers throwing error

我有這個程序,它只是C ++的更新,我一直在通過重載的運算符<<獲取要嘗試打印的指針的地址。 這是所有源代碼...

驅動程序

#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應該在vector<ToolB *> army中為派生類( PaperRockScissors )的所有實例調用cout ,但是當我編譯並運行程序,我得到以下輸出:

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

我知道解決辦法; 但是,我的指令專門說在EXCEPT ToolB所有類中創建cout重載。

我已經嘗試了幾乎所有內容,沒有任何東西可以提供所需的輸出。

謝謝!

您正在打印指針,而不是它們指向的對象。 在使用cout << *v[i]而不是cout << v[i]將它們發送到cout之前,需要先取消對指針的引用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM