little_greg Posted June 28, 2010 Report Share Posted June 28, 2010 #ifndef VEC4_H_#define VEC4_H_ #include <iostream> class Vec4 { private: float* vec; public: Vec4(int num); float operator[](int num); }; #endif #include "vec4.h"Vec4::Vec4(int num) { vec = new float[num]; for (int i = 0; i < num; i++) vec = 0; } float Vec4::operator[](int num) { return vec[num]; } #include "vec4.cpp"int main() { Vec4 v4(4); std::cin >> v4[2]; //error: no match for 'operator>>' in 'std::cin >> v4.Vec4::operator[](2)'| std::cout << v4[2]; return 0; } возникает ошибка при попытке записать значение в массив объекта, типа не перегружена операция >>, но перегружена операция индексации и ведь по идее, v4[2] - уже имеет тип float, а не Vec4, почему происходит такой косяк? Quote Link to comment Share on other sites More sharing options...
Darhazer Posted June 28, 2010 Report Share Posted June 28, 2010 imho operator[] должень возвращать референцию, иначе как вы запишете вход потребителя? Quote Link to comment Share on other sites More sharing options...
little_greg Posted June 28, 2010 Author Report Share Posted June 28, 2010 спс, уже сам понял Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.