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 answer 1

    You need to dereference the pointer:

     cout << (*v1)[index]; 

    Or call the operator as a function:

     cout << v1->operator [](index); 
    • *v1[index] equivalent to *(v1[index]) , maybe you meant (*v1)[index] . - PetSerAl