template<typename Type> double & List_Two_Link<Type>::operator[](int Var_for_search) { List_Two_Link * rec = this->pHead; for (int i = 0; (i <= this->itAmount) && (rec->itIndex != Var_for_search); i++, rec = rec->pNext) { if (i == this->itAmount) { cout << "Object not found" << endl; return NULL; } } return rec->pDate->Get_Result() // Здесь функция возвращает double; } bool Menu(List_Two_Link<Polynom> *& obj) { double Rec = obj[1]; //здесь ошибка ... } 

Tell me how to override operator[] for a doubly linked list. So far for the code above, this error crashes:

There is no suitable conversion function from "List_Two_Link" to "double"

  • Some rules ... I do not quite understand what you mean. pDate (type Polynom, it has some values ​​that are returned using Get_Result() type double) - Alexander Minyaev
  • double Rec = obj [1]; - Alexander Minyaev

1 answer 1

The [] operator for the pointer is expanded as *(pointer + index)
These records are fully equivalent.

 int *pointer; pointer[1] = 42; 1[pointer] = 42; *(pointer + 1) = 42; *(1 + pointer) = 42; 

The problem is what you are trying to apply to 'operator []'.

List_Two_Link<Polynom> *& obj

This is a pointer link. If to paint a line

double Rec = obj[1];

That will turn out

double Rec = *(obj + 1);

That is, you shift the pointer to 1 and dereference it. The result is an object of type List_Two_Link<Polynom> .
There are two ways to solve it. The first is to change the signature of the function, and take a reference to an object, not a pointer to an object.

bool Menu(List_Two_Link<Polynom> &obj)

The second way is to apply the operator [] not to the pointer, but to the object

double Rec = (*obj)[1];

  • one
    Or like obj->operator[](1) :) - Harry