Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Hi I am trying to overload >> operator for custom templated vector class but getting error Undifined Type 'T'

Hi I am trying to overload >> operator for custom templated vector class but getting error Undifined Type 'T'

Scheduled Pinned Locked Moved C / C++ / MFC
graphicshelp
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vinay108
    wrote on last edited by
    #1

    istream& operator>>(istream& cin, Vector& v)
    {

    cout << "input size" << endl;
    int n ;
    cin >> n;
    cout << "intput vector" << endl;
    for (int i = 0; i < n; i++)
    {
    	T data;
    	cin >> data;
    	v.push\_back(data);
    }
    return cin;
    

    }
    int main()
    {
    Vector v(5);

    cin>>v;
    

    }
    Getting error undefined type T data;

    Greg UtasG Mircea NeacsuM S 3 Replies Last reply
    0
    • V vinay108

      istream& operator>>(istream& cin, Vector& v)
      {

      cout << "input size" << endl;
      int n ;
      cin >> n;
      cout << "intput vector" << endl;
      for (int i = 0; i < n; i++)
      {
      	T data;
      	cin >> data;
      	v.push\_back(data);
      }
      return cin;
      

      }
      int main()
      {
      Vector v(5);

      cin>>v;
      

      }
      Getting error undefined type T data;

      Greg UtasG Offline
      Greg UtasG Offline
      Greg Utas
      wrote on last edited by
      #2

      If your intention is to use std::vector, your problem is that it shouldn't be capitalized.

      Robust Services Core | Software Techniques for Lemmings | Articles
      The fox knows many things, but the hedgehog knows one big thing.

      <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
      <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

      V 1 Reply Last reply
      0
      • V vinay108

        istream& operator>>(istream& cin, Vector& v)
        {

        cout << "input size" << endl;
        int n ;
        cin >> n;
        cout << "intput vector" << endl;
        for (int i = 0; i < n; i++)
        {
        	T data;
        	cin >> data;
        	v.push\_back(data);
        }
        return cin;
        

        }
        int main()
        {
        Vector v(5);

        cin>>v;
        

        }
        Getting error undefined type T data;

        Mircea NeacsuM Offline
        Mircea NeacsuM Offline
        Mircea Neacsu
        wrote on last edited by
        #3

        Either you make your function a template:

        template istream& operator>>(istream& cin, vector& v)
        {
        ...

        or you make it a specific function for vectors of strings:

        istream& operator>>(istream& cin, vector& v)
        {

        cout << "input size" << endl;
        int n;
        cin >> n;
        cout << "intput vector" << endl;
        for (int i = 0; i < n; i++)
        {
        	string data;
        	cin >> data;
        	v.push\_back (data);
        }
        return cin;
        

        }

        Either solution works. (that's beside Greg's observation about capitalization.)

        Mircea

        V 1 Reply Last reply
        0
        • Greg UtasG Greg Utas

          If your intention is to use std::vector, your problem is that it shouldn't be capitalized.

          Robust Services Core | Software Techniques for Lemmings | Articles
          The fox knows many things, but the hedgehog knows one big thing.

          V Offline
          V Offline
          vinay108
          wrote on last edited by
          #4

          No, actually I am trying to build a custom vector class.

          Greg UtasG 1 Reply Last reply
          0
          • Mircea NeacsuM Mircea Neacsu

            Either you make your function a template:

            template istream& operator>>(istream& cin, vector& v)
            {
            ...

            or you make it a specific function for vectors of strings:

            istream& operator>>(istream& cin, vector& v)
            {

            cout << "input size" << endl;
            int n;
            cin >> n;
            cout << "intput vector" << endl;
            for (int i = 0; i < n; i++)
            {
            	string data;
            	cin >> data;
            	v.push\_back (data);
            }
            return cin;
            

            }

            Either solution works. (that's beside Greg's observation about capitalization.)

            Mircea

            V Offline
            V Offline
            vinay108
            wrote on last edited by
            #5

            Yeah, Thanks!

            1 Reply Last reply
            0
            • V vinay108

              No, actually I am trying to build a custom vector class.

              Greg UtasG Offline
              Greg UtasG Offline
              Greg Utas
              wrote on last edited by
              #6

              Then see Mircea's first solution below, but keep Vector capitalized.

              Robust Services Core | Software Techniques for Lemmings | Articles
              The fox knows many things, but the hedgehog knows one big thing.

              <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
              <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

              1 Reply Last reply
              0
              • V vinay108

                istream& operator>>(istream& cin, Vector& v)
                {

                cout << "input size" << endl;
                int n ;
                cin >> n;
                cout << "intput vector" << endl;
                for (int i = 0; i < n; i++)
                {
                	T data;
                	cin >> data;
                	v.push\_back(data);
                }
                return cin;
                

                }
                int main()
                {
                Vector v(5);

                cin>>v;
                

                }
                Getting error undefined type T data;

                S Offline
                S Offline
                Stefan_Lang
                wrote on last edited by
                #7

                When using already existing container types from the STL, just use the container type as template parameter. You can retrieve the element type with the internal type definition value_type like this:

                template
                istream& operator>>(istream& my_istream, Container& v)
                {
                typedef typename Container::value_type T; // now you can use T for your element type
                ...

                If you define your own container type, you can do it in the same way. You just have to make sure your container type does define value_type:

                template
                class MyContainer {
                public:
                typedef Element value_type;
                ...

                P.S.: IIRC STL containers already do have overrides for operator>>. You could save yourself some effort by just overriding this operator for your base type:

                template
                istream& operator>>(istream& my_istream, T& value) {
                ...

                It should work just as well as the above solution. P.P.S.: As it turns out I was wrong. There are no default implementations of operator>> for containers.

                GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups