fork(1) download
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <string>
  4. #include <vector>
  5. template <typename T>
  6. class Myclass {
  7. std::vector<T> s(10);
  8. public:
  9. T& operator[](int i) {
  10. if(i<0 || i>9)
  11. {
  12. std::cout<<"Index value is out of bounds\n";
  13. std::exit(1);
  14. }
  15. return s[i];
  16. }
  17. };
  18. int main(){
  19. Myclass<std::string> s;
  20. s[0]="Shreya";
  21. s[1]="Meet";
  22. s[2]="Pia";
  23.  
  24. for(int i=0;i<3;i++)
  25. std::cout<<s[i]<<' ';
  26. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:7:19: error: expected identifier before numeric constant
  std::vector<T> s(10);
                   ^
prog.cpp:7:19: error: expected ',' or '...' before numeric constant
prog.cpp: In instantiation of 'T& Myclass<T>::operator[](int) [with T = std::basic_string<char>]':
prog.cpp:20:5:   required from here
prog.cpp:15:12: error: invalid use of member function (did you forget the '()' ?)
    return s[i];
            ^
stdout
Standard output is empty