首页 > 解决方案 > 关于未定义符号的问题: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;
}

标签: c++templatesinheritanceoperator-overloading

解决方案


推荐阅读