c++ - 关于未定义符号的问题:operator<<
问题描述
我正在尝试重载运算符 << 以在对象上显示内容,但我需要在几个不同的类中使用它,因此我制作了运算符 << 和 showStatus() 函数模板。
IDE 显示以下错误:
Undefined symbol: operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Mege const&)
不确定它是什么。
我删除了一些不相关的代码,所以这个问题不会发布太多代码。让我知道是否需要更多详细信息。
帮助将不胜感激。
class Hero : public Entity{
private:
public:
template <class T>
void showStatus(T){}
};
#include "Hero.hpp"
#include <math.h>
Hero::Hero(){
}
// overloading << for showing different hero's status
template <class T>
std::ostream &operator << (std::ostream &strm, const T &hero){
strm << hero.getName() << endl
<< "-------------------------------"
<< "Level: " << right << setw(4) << hero.getLevel() << endl
<< "EXP: " << hero.getExp() << " / " << hero.getExpNext() << endl
<< "HP: " << hero.getCurrHp() << " / " << hero.getMaxHp() << endl
<< "Damage: " << hero.getMinDamage() << " ~ " <<hero.getMaxDamage() << endl
<< "-------------------------------"
<< "Strength: " << hero.getStrength() << endl
<< "Intelligence: "<< hero.getInterlligence() << endl
<< "Stamina: " << hero.getStamina() << endl
<< "Speed: " << hero.getSpeed() << endl
<< "-------------------------------" << endl << endl;
return strm;
}
#include <stdio.h>
#include <iostream>
#include "Hero.hpp"
class Mege : public Hero{
private:
public:
Mege();
~Mege();
// function
friend std::ostream &operator << (std::ostream &strm, const Mege &hero);
void updateStatus() override;
void showStatus(Mege) ;
// setter
// getter
};
#include "Mege.hpp"
#include <math.h>
std::ostream &operator << (std::ostream &strm, const Mege &mege);
void Mege::showStatus(Mege mege){
cout << mege;
}
解决方案
推荐阅读
- javascript - 数据字符串如何包裹在里面的对象上?工作到 html 显示
- c# - 如何使用 string.Format() 将占位符替换为 HTML 电子邮件模板中的动态文本
- python - End of line error appearing in the end of the conditional statement
- python - Python: access a variable in a module from another file
- python - Runge-Kutta method overflow encountered in double_scalars
- javascript - JavaScript 中带有尾部的 LinkedList push() 方法
- android - Android 11 background FCM with data only and screen off
- c - why can't I initialize a linked list like this?
- jmeter - JMeter altering values in .xls files using bean shell processor
- react-native - react native gesture touch point capture issue