overload operaror <<

I'm confused about how to use overload operator, My assignment is: You should use the overload operator << so that the printing of the vector can achieved by
cout << v << endl;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <iomanip>
#include <ctime>
#include <vector>
#include <algorithm>
using namespace std;
const int SIZE =10;
unsigned int seed = (unsigned)time(0);
struct node
{
    int integer;
    double value;
};
typedef node structure_node;
double random(unsigned int&seed);
void print_vector(const vector<node> &v );
void initialize_vector(vector<node> &v);
ostream & operator << (ostream & out, structure_node v);
void output_node(ostream &out,structure_node v);
int main()
{
    vector<node> v(SIZE);
    structure_node p;
    initialize_vector(v);
    cout << "Create and print a simple structure node" << endl;
    p.integer= v[0].integer +3;
    cout << p.integer << "    ";
    cout << v[0].value << endl;
    cout << "Example of using overloading of << for printing nodes" << endl;
    for (int i=0; i<v.size(); ++i)
        cout << v[i];
    cout << endl;
    
    return 0;
}
double random(unsigned int &seed)
{
    const int MODULUS = 15749;
    const int MULTIPLIER = 69069;
    const int INCREMENT = 1;
    seed = ((MULTIPLIER*seed)+INCREMENT)%MODULUS;
    return double (seed)/MODULUS;
}
void initialize_vector(vector<node> &v)
{
    for (int i=0; i<SIZE; i++)
    {
        v.push_back(node());
        v.at(i).integer = int(10*random(seed)) ;
        v.at(i).value = double(10*random(seed));
    }
}
void print_vector ( const vector<node> &v )
{
    for (int i=0; i<v.size(); i++)
    {
        cout<< v.at(i).integer << " " ;
        cout << v.at(i).value << endl;
    }
}
ostream &operator << (ostream &out, structure_node v)
{
    output_node(out, v);
    return (out);
}
void output_node(ostream &out, structure_node v)
{
    cout << fixed << showpoint << setprecision(3);
    cout << setw(2) << v.integer << "-" ;
    cout << setw(3)<< v.value << endl;
}
1
2
3
4
5
6
void output_node(ostream &out, structure_node v)
{
    cout << fixed << showpoint << setprecision(3);
    cout << setw(2) << v.integer << "-" ;
    cout << setw(3)<< v.value << endl;
}

Notice that you passed in the "out" ostream here, but never used it.
Do out << stuff; not cout << stuff; inside of your output logic.
This actually won't matter when just normally using the function with standard out, but would matter if you're passing in an ofstream or another kind of ostream somewhere else in the program.

I'm not sure if that answers your question. Is there a particular issue you're trying to resolve, other than that?
Last edited on
You should use the overload operator << so that the printing of the vector can achieved by
cout << v << endl;

If v is a vector<node>, then you do need an operator<< that takes ostream and vector<node> as operands.
Topic archived. No new replies allowed.