Cannot Solve Error

I am trying to overload the [] operator.The problem I am getting is that it either sys that It excepts one argument or it must be a non-static member function.Tried to make it a stand-alone function, a friend function, but I still fail. Can someone please help.

This is when I make it a standalone function.The error is must be non-static.When I make it a friend, the error is Except only one argument
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  void operator[]( int  value, int _index)
{
	//int _index;
	if(_index < 0 || _index >= size)
	{
		cout<<" Error 404 "<<endl; 
		exit(EXIT_FAILURE);
	}
	else 
	{
		ptr[_index] = value;
	}
}
I suggest making the overload a member function. Anyway, posting the class would make things a bit easier.

Aceix.
Everything id fine the problem is overloading the [] operator with two arguments.I did the one with one argument and it worked just fine

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


int Array :: operator[](int subscript)const // return the value at subscript if found
{
	if(subscript < 0 || subscript >= size)
	{
		cout<<" Error 404"<<endl;
		exit(EXIT_FAILURE);
	}
	else
	{
		return ptr[subscript];
	}
}

void operator[]( int  value, int _index) // store the value at the index specified
{
	//int _index;
	if(_index < 0 || _index >= size)
	{
		cout<<" Error 404 "<<endl; 
		exit(EXIT_FAILURE);
	}
	else 
	{
		ptr[_index] = value;
	}
}





In the first one I return the value provided its in the range specifed.But in the second one I need to store the value passed in the index passed.That is the problem I am having...


Please help....
> But in the second one I need to store the value passed in the index passed.
> That is the problem I am having...

The subscript operator can return a reference to an assignable object.

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
#include <iostream>
#include <cstdlib>

struct A
{
    int& operator[] ( std::size_t pos )
    {
        if( pos >= SZ ) error_exit(404) ;
        return a[pos] ;
    }

    const int& operator[] ( std::size_t pos ) const
    {
        if( pos >= SZ ) error_exit(404) ;
        return a[pos] ;
    }

    static constexpr std::size_t SZ = 10 ;

    private:

        int a[SZ] = {0} ;

        static void error_exit( int e )
        {
            std::cerr << " Error " << e << '\n' ;
            std::exit(EXIT_FAILURE); // not a great idea
        }
};

int main()
{
    A a ;
    a[4] = 23 ;
}
okay, I think I get your code, but then I get lost on the way.
What I want to chieve is change the number.

I simply want to store the value at the index. eg suppose the arguments passed are value = 10 && _index = 4; then ptr[4] should be equal to 10;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

void operator[]( int  value, int _index) // store the value at the index specified
{
	//int _index;
	if(_index < 0 || _index >= size)
	{
		cout<<" Error 404 "<<endl; 
		exit(EXIT_FAILURE);
	}
	else 
	{
		ptr[_index] = value;
	}
}



> What I want to chieve is change the number.

This a[4] = 23 ; (line 34) is changing the number from 0 to 23.
All operators have a fixed number of parameters. You cannot write
1
2
3
4
void operator+(FOO f, BAZ s, BAR& dest);
//nor
YourClass A;
A[4, 5];


Think about how you normally use arrays:
1
2
int a[5];
a[3] = 5;


Your object should look the same:
 
A[5] = 4;


Overloaded subscript operators usually return a reference to the item at that index, and that item can be modified. That way, you don't have to do any assignments internally. It also let's you do:
1
2
3
A[0] =1;
A[0] *= 4;
cin >> A[2];
I tried it but i get

1
2
3
4
5
main.cpp:20:6: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second: [enabled by default]
In file included from main.cpp:1:0:
array.h:35:7: note: candidate 1: int Array::operator[](int) const
array.h:40:8: note: candidate 2: int& Array::operator[](size_t)
main.cpp:20:10: error: lvalue required as left operand of assignment


May someone please explain to me what certain things do in that code and how.Cause I cannot seem to figure them out.
array.h:35:7: note: candidate 1: int Array::operator[](int) const
array.h:40:8: note: candidate 2: int& Array::operator[](size_t)


Overload on the const qualifier, but not on the integer type of the argument.

1
2
int Array::operator[](int size_t ) const
int& Array::operator[](size_t)
But when I tried to make the assignment in Mai I get the lvalue required as left operand...Please show me where I am going wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int *ptr;
		int size;
		int now_size;
	
	//int a[size] = {0};
	
	public:
		
		Array();
		Array(const int*, int);
		~Array();
		Array(const Array &);
	
		
		int operator[] (int);
	
		const Array& operator = (const Array&);
		
		static const  std::size_t SZ = 10 ;
		int &operator[]( size_t);
		



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


int Array :: operator[](int subscript)
{
	if(subscript < 0 || subscript >= size)
	{
		cout<<" Error 404 "<<endl; 
		exit(EXIT_FAILURE);
	}
	else
	{
		return ptr[subscript];
	}
}

int& Array :: operator[]( size_t pos)
{
	//int _index;
	if(pos < 0 || pos >= size)
	{
		cout<<" Error 404 "<<endl; 
		exit(EXIT_FAILURE);
	}
	else 
	{
		return ptr[pos];
	}
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14

#include "array.h"
#include <iostream>

using namespace std;

int main()
{
	int *array;
	Array ar(array, 9);
		ar[5] = 99; 
	
	return 0;
}


This is the error I get

1
2
3

main.cpp:21:10: error: lvalue required as left operand of assignment
Last edited on
Topic archived. No new replies allowed.