overloading operator>> for an array class

I'm trying to create an array class with functions and useful operators. But im stuck on the input method, my initial design would go like this:
1
2
3
4
Array<double,12> arr; //declare array object
arr >> 12 >> 15 >> 17; //read values into object
double cars[5] = {....}
arr >> cars //read arrays/objects in(overloading) 


how would i go about the function? im confused on whether to declare it as a friend function to return an istream object or to return an array object, thanks for the help! for quick reference, skip to line 73 for the function i need help on.

_Array.h
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
72
73
74
75
76
77
#ifndef _ARRAY_H
#define _ARRAY_H
#include <vector>
#include <iostream>
template<class T, int n>
class Array
{
    private:
        T arr[n];
        int len;
    public:
        Array();
        Array(const Array & arr);
        Array & operator=(const Array & a);
        friend std::istream operator>>(std::istream is,const T & var);
        T & operator[](int i) const;
        void show();
};

template<class T, int n>
Array<T,n>::Array()
{
    len = n;
    for(int i = 0;i < len;i++)
        arr[i] = (T)0;
}
template<class T, int n>
Array<T,n>::Array(const Array & arr)
{
    if(len != arr.len)
        std::cout << "arrays of different lengths, incompatable...nothing to be done..\n";
    else{
    for(int i = 0;i < len;i++)
        arr[i] = arr.arr[i];
    }
}
template<class T, int n>
Array<T,n> & Array<T,n>::operator=(const Array & a)
{
    if(len != arr.len)
        std::cout << "arrays of different lengths, incompatable...nothing to be done..\n";
    else{
    for(int i = 0;i < len;i++)
        arr[i] = arr.arr[i];
    }
}
template<class T,int n>
void Array<T,n>::show()
{
    int counter = 0;
    std::cout << "Contents of array: \n";
    std::cout << "Length: " << len << std::endl;
    std::cout << "Data: \n";
    for(int i = 0; i < len;i++)
    {
        counter++;
        std::cout << arr[i] << ",";
        if(counter ==3){
            std::cout << std::endl;
            counter = 0;
        }
    }

}
template<class T, int n>
T & Array<T,n>::operator[](int i)
{
    return arr[i];
}
template<class T, int n>
friend std::istream operator>>(std::istream is,const T & var)
{
    //stuck here, no clue what to do!
}

#endif // _ARRAY_H

Last edited on
could use some help!
1
2
Array<double,12> arr; //declare array object
arr >> 12 >> 15 >> 17; //read values into object 

That isn't conventional use of >>. Are you sure that's what you want to do?

You showed this example

arr >> cars //read arrays/objects in(overloading)

but this function have nothing common with your class as an operand of the function.

So what are you going to do? Do you want that values from an object of you class would be assigned to other objects of scalar types or of arrays?
Last edited on
Topic archived. No new replies allowed.