ostream& does not name a type error. What am I doing wrong here?

I am a learner. I am working on operator overloading. I am trying to write a code for overloading [] and print the elements in the member array. But when I am overloading << to print the member array, I get the error, ostream& does not have a type. What am I doing wrong here? Also what can I do if I have a class that has two member arrays? Here is my code below:

#include <iostream>
#include <cassert>
class Digit
{
private:
    int digit1[3]{0};

public:    
    int& operator[](const int index);

    ostream& operator<<(ostream& out);
};

int& Digit::operator[](const int index)
{
    return digit1[index];
}

ostream& Digit::operator<<(ostream& out)
{
    int loop;
    out << "{";
    for (loop = 0; loop < 10; loop++)
    {
        out << digit1[loop] << " ";
    }
    out << "}";

    return o;
}

int main()
{
    using namespace std;

    Digit n;
    n[0] = 4;
    n[1] = 3;
    n[2] = 4;


    n << cout;

    return 0;
}

Answers


You have put

int main()
{
    using namespace std;
    //....

This cannot be seen where you declare your << operator. One solution is to change the signature to include the namesape:

In the class:

std::ostream& operator<<(std::ostream& out)

and then

std::ostream& Digit::operator<<(std::ostream& out)

While you are there, I wonder if it should be const?


Need Your Help

GridView entries are being added from the bottom up

java android eclipse gridview adt

I want to add entries to my GridView starting at the top, but instead the first entry is registered at the bottom left. I have another piece of code in my project that adds entries to a GridView an...

trying to freeze the left column

css html

I would like the left column fixed when I scroll to the right.