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, почему происходит такой косяк? Link to comment Share on other sites More sharing options...
Darhazer Posted June 28, 2010 Report Share Posted June 28, 2010 imho operator[] должень возвращать референцию, иначе как вы запишете вход потребителя? Link to comment Share on other sites More sharing options...
little_greg Posted June 28, 2010 Author Report Share Posted June 28, 2010 спс, уже сам понял Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now