Skip to main content
added 75 characters in body
Source Link
David G
  • 97.6k
  • 41
  • 173
  • 258

You can't iterate over an object's data members. You can use std::ostream's stream insertion operator to print individually however:

struct A
{
    int a;
    int b;
    std::string c;

    friend std::ostream& operator <<(std::ostream& os, A const& a)
    {
        return os << a.a << '\n'
                  << a.b << '\n'
                  << a.c << '\n';
    }
};

And inside main:

int main()
{
    A a = {5, 10, "apple sauce"};

    std::cout << a;
}

Output:

5
10
apple sauce

Here is a demo.

You can't iterate over an object's data members. You can use std::ostream's stream insertion operator to print individually however:

struct A
{
    int a;
    int b;
    std::string c;

    friend std::ostream& operator <<(std::ostream& os, A const& a)
    {
        return os << a.a << '\n'
                  << a.b << '\n'
                  << a.c << '\n';
    }
};

And inside main:

int main()
{
    A a = {5, 10, "apple sauce"};

    std::cout << a;
}

Output:

5
10
apple sauce

You can't iterate over an object's data members. You can use std::ostream's stream insertion operator to print individually however:

struct A
{
    int a;
    int b;
    std::string c;

    friend std::ostream& operator <<(std::ostream& os, A const& a)
    {
        return os << a.a << '\n'
                  << a.b << '\n'
                  << a.c << '\n';
    }
};

And inside main:

int main()
{
    A a = {5, 10, "apple sauce"};

    std::cout << a;
}

Output:

5
10
apple sauce

Here is a demo.

Source Link
David G
  • 97.6k
  • 41
  • 173
  • 258

You can't iterate over an object's data members. You can use std::ostream's stream insertion operator to print individually however:

struct A
{
    int a;
    int b;
    std::string c;

    friend std::ostream& operator <<(std::ostream& os, A const& a)
    {
        return os << a.a << '\n'
                  << a.b << '\n'
                  << a.c << '\n';
    }
};

And inside main:

int main()
{
    A a = {5, 10, "apple sauce"};

    std::cout << a;
}

Output:

5
10
apple sauce