[C ++在类中正确使用ostream并传递参数?

问题描述 投票:0回答:1

我最近开始学习c ++,为了我的一生,我似乎无法获得在ostream中使用class的语法以及应该传递的参数。这是代码:

这是有问题的课程:

#include <iostream>
#include <string>

using namespace std;


class Pokemon{
    friend ostream& operator<<(ostream&, Pokemon);
public:
    string name, level, cp;

    Pokemon(string x="Pikachu", string y="5", string z="1000"){
        name = x;
        level = y;
        cp = z;

    }

    Pokemon name(){
        return this->name;
    } 

    Pokemon level(){
        return this->level;
    }

    Pokemon cp(){
        return this->cp;
    }

    Pokemon display_stats(){
        cout << this-> name << "stats are:" << endl;
        cout << "         " << "Attack: 2716.05" << endl;
        cout << "         " << "Defence: 1629.63" << endl;
        cout << "         " << "HP: 1086.42" << endl;

    }

};

template<typename TYPE> //i dont understand this and the things i've written down here are only based on samples i've seen
ostream& operator<<(ostream& os, Pokemon & c){
    os << "The level of " << c.name << " is" << c.level << " with cp of " << c.cp;
}

如您所见,我已经尝试构建ostream的东西,但是我不太了解它是如何工作的。这是我的主要功能:

int main()
{
    Pokemon a, b, c, d;

    a = Pokemon();
    b = Pokemon("Weezing");
    c = Pokemon("Nidoking", 100);
    d = Pokemon("Mewtwo", 50, 5432.1);

    cout << a << endl;
    cout << b << endl;
    cout << c << endl;
    cout << d << endl;
    cout << "Jessie: You are no match to me! Go " << b.name << "!" << endl;
    cout << "Gary: Go lvl " << c.level << " " << c.name << "! Crush them" << endl;
    cout << "Ash: " << a.name << " can do it even thouh he is only level " << a.level << endl;
    cout << "Jessie: Hahaha! My " << b.name << " CP is " << b.cp << endl;
    cout << "Gary: "<< c.name << " CP is " << c.cp << endl;
    cout << "Ash: " << a.name << " CP is " << a.cp << endl;
    cout << "Giovanni: Behold " << d.name << " is here." << endl;
    d.display_stats();


    return 0;
}

我收到以下错误:

no instance of constructor "Pokemon::Pokemon" matches the argument list -- argument types are: (const char [9], int) //on line c = Pokemon("Nidoking", 100);
no instance of constructor "Pokemon::Pokemon" matches the argument list -- argument types are: (const char [7], int, double) //on line d = Pokemon("Mewtwo", 50, 5432.1);
c++ arguments ostream
1个回答
0
投票

您的所有Pokemon类方法都返回了错误的类型。而且您的main()根本没有正确调用任何方法。

更改您的Pokemon类,使其看起来像这样:

#include <iostream>
#include <string>
using namespace std;

class Pokemon {
private:
    string m_name;
    int m_level;
    double m_cp;

    friend ostream& operator<<(ostream&, const Pokemon&);

public:

    Pokemon(string x="Pikachu", int y=5, double z=1000) {
        m_name = x;
        m_level = y;
        m_cp = z;
    }

    string name() const {
        return m_name;
    } 

    int level() const {
        return m_level;
    }

    double cp() const {
        return m_cp;
    }

    void display_stats() const {
        cout << m_name << " stats are:" << endl;
        cout << "         " << "Attack: 2716.05" << endl;
        cout << "         " << "Defense: 1629.63" << endl;
        cout << "         " << "HP: 1086.42" << endl;
    }
};

ostream& operator<<(ostream& os, const Pokemon &c) {
    os << "The level of " << c.m_name << " is " << c.m_level << " with cp of " << c.m_cp;
    return os;
}

然后更改main()看起来更像这样:

int main()
{
    Pokemon a;
    Pokemon b("Weezing");
    Pokemon c("Nidoking", 100);
    Pokemon d("Mewtwo", 50, 5432.1);

    cout << a << endl;
    cout << b << endl;
    cout << c << endl;
    cout << d << endl;

    cout << "Jessie: You are no match to me! Go " << b.name() << "!" << endl;
    cout << "Gary: Go lvl " << c.level() << " " << c.name() << "! Crush them" << endl;
    cout << "Ash: " << a.name() << " can do it even though he is only level " << a.level() << endl;
    cout << "Jessie: Hahaha! My " << b.name() << " CP is " << b.cp() << endl;
    cout << "Gary: " << c.name() << " CP is " << c.cp() << endl;
    cout << "Ash: " << a.name() << " CP is " << a.cp() << endl;
    cout << "Giovanni: Behold " << d.name() << " is here." << endl;
    d.display_stats();

    return 0;
}

Live Demo

© www.soinside.com 2019 - 2024. All rights reserved.