[英]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;
}
Paper
和Scissors
类与Rock
类完全相同,除了一些较小的值更改外,因此我没有发布该代码。
在Driver.cpp中,ToolB的静态方法displayToolBs
应该在vector<ToolB *> army
中为派生类( Paper
, Rock
和Scissors
)的所有实例调用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.