-2

I have a simple problem and I need a hint.

I have a program like this:

#include <iostream>
#include <string>
#include <stack>

using namespace std;

stack<int> stack1;

int main() {
    stack1.push(11);
    cout << stack1.top() << endl;
    system("Pause");
    return 0;
}

Which is working perfect:

image

But I need to use a class/struct instead of an int, and this is not working so well.

#include <iostream>
#include <string>
#include <stack>

using namespace std;

struct player {
    string name;
    string surname;
    int age;
};

stack<player> stack1;

int main() {
    stack1.push("Zlatan","Ibrahimovic",11); //I do not know how to do it properly.
    cout << stack1.top() << endl;
    system("Pause");
    return 0;
}

Error in Visual Studio:

image

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. Always post error messaages verbatim as text, not as images. – πάντα ῥεῖ May 16 '17 at 17:37
  • 1
    Use `stack1.push(player{"Zlatan","Ibrahimovic",11})` – πάντα ῥεῖ May 16 '17 at 17:38
  • 1
    You need to [overload `operator<<`](http://stackoverflow.com/questions/476272/how-to-properly-overload-the-operator-for-an-ostream) if you want to be able to output a `struct` like that, otherwise who knows how you're suppose to print that type? – François Andrieux May 16 '17 at 17:41
  • If i understand correctly overload operator, should I change data type to const and use "&"? – scooby.doo123 May 16 '17 at 18:09
  • @πάνταῥεῖ Please, don't use the comments section for answering. – Lightness Races in Orbit May 16 '17 at 19:14

1 Answers1

2

You need to construct an instance of your player struct before you can then push it into the stack.

Also, operator<< doesn't know how to stream your player struct by default, so you need to overload operator<< to provide that functionality yourself.

If are you using a version of C++ prior to C++11, try this:

#include <iostream>
#include <string>
#include <stack>
#include <cstdlib>

struct player
{
    std::string name;
    std::string surname;
    int age;

    player(const std::string &name, const std::string &surname, int age)
        : name(name), surname(surname), age(age)
    {
    }

    void print(std::ostream &out) const
    {
        out << "Name: " << name << " " << surname << ", Age: " << age;
    }
};

std::ostream& operator<<(std::ostream &out, const player &p)
{
    p.print(out);
    return out;
}

std::stack<player> stack1;

int main()
{
    stack1.push(player("Zlatan", "Ibrahimovic", 11));
    std::cout << stack1.top() << std::endl;
    std::system("Pause");
    return 0;
}

Live Demo

If are you using C++11 or later, try this instead:

#include <iostream>
#include <string>
#include <stack>
#include <cstdlib>

struct player
{
    std::string name;
    std::string surname;
    int age;

    void print(std::ostream &out) const
    {
        out << "Name: " << name << " " << surname << ", Age: " << age;
    }
};

std::ostream& operator<<(std::ostream &out, const player &p)
{
    p.print(out);
    return out;
}

std::stack<player> stack1;

int main()
{
    stack1.push(player{"Zlatan", "Ibrahimovic", 11});
    std::cout << stack1.top() << std::endl;
    std::system("Pause");
    return 0;
}

Live Demo

Alternatively:

#include <iostream>
#include <string>
#include <stack>
#include <cstdlib>

struct player
{
    std::string name;
    std::string surname;
    int age;

    player(const std::string &name, const std::string &surname, int age)
        : name(name), surname(surname), age(age)
    {
    }

    void print(std::ostream &out) const
    {
        out << "Name: " << name << " " << surname << ", Age: " << age;
    }
};

std::ostream& operator<<(std::ostream &out, const player &p)
{
    p.print(out);
    return out;
}

std::stack<player> stack1;

int main()
{
    stack1.emplace("Zlatan", "Ibrahimovic", 11);
    std::cout << stack1.top() << std::endl;
    std::system("Pause");
    return 0;
}

Live Demo

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770