Function call operator

Hey guys,

I am struggling to find my way out of this situation. I just want to call the [] operator within the () operator, which will be finally used/written on the screen with the ostream << operator. Whenever I'm using the sintax _elements[index] the execution crashes with the error vector out of range.

Do you have any ideea what should be the correct code ?


Header File:

typedef unsigned int Uint;
typedef vector<Uint> TVint;
typedef vector<Uint>::const_iterator Titerator;

class Sir
{
protected:
Uint _count;
TVint _elements;
public:
int index;
Sir():_count(0){}
Uint operator [] (int index);
Sir& operator() (int index);
virtual string Name() const=0;
virtual ~Sir(){}
virtual void CalculateValues(int index)=0;
protected:
friend ostream& operator<<(ostream &out, const Sir&sir);
};

CPP File:

Uint Sir:: operator [] (int index)
{
if (index <0)
{
throw outofrange();
}
_count=index+1;
CalculateValues(index);
return _elements[_count];
}


Sir& Sir::operator() (int index)
{
_elements[index]; ?????????????????? - What is wrong with this expression ?
return *this;
}

what happens if you try the .at() method on a vector, rather than the [] operator?


edit:
wait a minute..
What is wrong with this expression ?

what exactly is not working?
It compiles on my machine.
you do realise you cannot instantiate a Sir object as you've made it abstract?
Last edited on
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
Header File:

typedef unsigned int Uint;
typedef vector<Uint> TVint;
typedef vector<Uint>::const_iterator Titerator;

class Sir
{
protected:
Uint _count;
TVint _elements;
public:
int index;
Sir():_count(0){}
Uint operator [] (int index);
Sir& operator() (int index);
virtual string Name() const=0;
virtual ~Sir(){}
virtual void CalculateValues(int index)=0;
protected:
friend ostream& operator<<(ostream &out, const Sir&sir);
};

CPP File:


ostream& operator<<( ostream &out, const Sir&sir)
{
for(Uint i=0;i<sir._count;++i)
{
out<<"................"<<endl;
out<<i<<"\t"<<sir._elements[i]<<endl;
}
return out;
};


Uint Sir:: operator [] (int index)
{
if (index <0)
{
throw outofrange();
}
_count=index+1;
CalculateValues(index);
return _elements[_count];
}


Sir& Sir::operator() (int index)
{	
_elements[index]; [output] has to call the [] operator [/output]
return *this;
}


Well the problem is that according to my project index parameter is calculated and validated within the CalculateValues(index) method and furthermore I have to call the [] operator from the () operator.

Whenever I am using the sintax _elemente[index] within the () operator, the execution fails ( not the debugging ) with the error : vector subscript out of range.

It is working if the code is duplicated as follows:

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

Uint Sir:: operator [] (int index)
{
if (index <0)
{
throw outofrange();
}
_count=index+1;
CalculateValues(index);
return _elements[_count];
}


Sir& Sir::operator() (int index)
{	
_count=index+1;
CalculateValues(index); [output] has to call the [] operator [/output]
return *this;
}



However it is mandatory that within the function () operator , the [] operator has to be called. But I can't figure out how shoudl that be performed, it's really a headache after all the sintax, this little bug is keeping me from the final call.

Many thanks!

Topic archived. No new replies allowed.