Operator overloading to read in an array

I want to read an array into a class i call _Array using the operator ">>", but im stuck as to overloading the function. Here's the code:
_Array.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef _ARRAY_H
#define _ARRAY_H
#include <vector>

class _Array
{
    public:
        _Array();
        int * _Reverse();
        int * _ASCII();
        bool Good();
        _Array operator>>(const int ar[]);
    protected:
    private:
    std::vector<int> Data;
};

#endif // _ARRAY_H


_Array.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "_Array.h"
#include <iostream>
#include <vector>
_Array::_Array()
{
    //ctor
}


_Array operator>>(const int ar[])
{
    for(int i = 0; i < (sizeof(ar)/sizeof(int));i++)
    {
        Data.push_back(ar[i]);
    }
}

main.cpp
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "_Array.h"
using namespace std;

int main()
{
    int n[4] = {4,3,2,1};
    _Array Obj;
    Obj >> n;
}


ERROR:
J:\C++\ArrayFunctions\_Array.cpp|10|error: '_Array operator>>(const int*)' must have an argument of class or enumerated type|
||=== Build finished: 1 errors, 0 warnings ===|


Thanks if you can help!
Last edited on
1
2
3
4
5
6
7
istream& operator >> ( istream& ins, _Array& a )
  {
  // read in your array data here
  ...
  // simple operators typically return the stream
  return ins;
  }

BTW, don't start your identifiers with an underscore. How about "Array", or "SleepyArray" or some such?

Hope this helps.
Last edited on
How would this read into the class though? is the class object also a istream object? Thanks for the help!
The declaration

_Array operator>>(const int ar[]);

is equivalengt to

_Array operator>>(const int *a );

So the expression

sizeof(ar)/sizeof(int)

is equivalent to

sizeof( int * ) / sizeof( int )

Usually sizeof( int * ) is equal to 4 ( on 32-bit platforms) that means that the result of the expression will be 1 (sizeof( int ) is also usually equal to 4).

If you are trying to "shift" the array into an object of your class then it will be more naturally to use the operator << instead of >>
Also you need know the size of the array.
Taking all this into acccount your operator will look the following way ( have removed the underscore in the class name)

1
2
3
4
5
6
template <size_t N>
Array & operator <<( int ( &ar )[N] )
{
	for ( auto x : ar ) Data.push_back( x );
	return ( *this );
}





Last edited on
both of these options just give me a ton of errors and confuse the living hell out of me..... i guess ill just head back in my book and try to re-learn again..
Apart from said above you need the scope and return:

1
2
3
4
5
6
7
8
_Array _Array::operator>>(const int ar[])
{
    for(int i = 0; i < (sizeof(ar)/sizeof(int));i++)
    {
        Data.push_back(ar[i]);
    }
    return *this;
}


But as said above it will not resolve (sizeof(ar)/sizeof(int))

You could write it like so (variant of vlad from moscow):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class _Array
{
    public:
        _Array();
        int * _Reverse();
        int * _ASCII();
        bool Good();

        template <size_t N>
        _Array &operator>>(const int (&ar)[N])
        {
            for(size_t i = 0; i < N;i++)
            {
                Data.push_back(ar[i]);
            }
            return *this;
        }


    protected:
    private:
    std::vector<int> Data;
};


I'd say >> is the wrong direction, since the value are stored in _Array I'd expect <<
Ah, I misread. (It was a long day for me.)

Why are you using operator <</>> overload for an assignment? Don't do that.

The way to do what you want is not obvious, but coder777 got it right:
1
2
3
4
5
6
7
8
9
class Array
{
  public:
    template <size_t N>
    Array& operator = ( const int (&ar)[N] )
    {
      ...
    }
}

[edit] But, I should add that this becomes inefficient in codespace quickly. You should just defer to a dedicated assignment function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Array
{
  public:
    Array& assign( const int* ar, size_t N )
    {
      for (size_t i = 0; i < N; i++)
      {
        Data.push_back(ar[i]);
      }
      return *this;
    }

    template <size_t N>
    inline
    Array& operator = ( const int (&ar)[N] )
    {
      return assign(ar,N);
    }
}

Hope this helps.
Last edited on
Topic archived. No new replies allowed.