0
int main() {

    int index, first;

    cout << "Please enter the first vector (first,second): ";
    cin >> first >> second;
    Vector* v1 = new Vector(first, second);
    cout << "The vector you've entered is: ";
    v1->print();
    cout << "\n";

    cout << "Please enter index: ";
    cin >> index;
    cout << v1[index];
    cout << endl;

    delete[] v1;
    return 0;
}




    int& Vector ::operator [] (int index) {
        if (index == 0) {
            return *first;
        }
        else if(index == 1){
            return *second;
        }
        else {
            cout << "Error" << endl;
        }
    }

1 ответ 1

2

Нужно разыменовать указатель:

cout << (*v1)[index];

Либо вызвать оператор как функцию:

cout << v1->operator [](index);
1
  • *v1[index] эквивалентно *(v1[index]), возможно Вы имели ввиду (*v1)[index].
    – user181245
    14 мая 2016 в 10:34

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.