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 ответ
Нужно разыменовать указатель:
cout << (*v1)[index];
Либо вызвать оператор как функцию:
cout << v1->operator [](index);
-
*v1[index]
эквивалентно*(v1[index])
, возможно Вы имели ввиду(*v1)[index]
. 14 мая 2016 в 10:34