how to use vector of user defined type(user defined class )

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abbas khoshnevi
    New Member
    • Oct 2010
    • 1

    how to use vector of user defined type(user defined class )

    hi

    I am using myclass to define a vector .
    but I can not write values of member variable 'a' .
    please tell me how I van use member variable 'a' in printf command.

    thanks a lot


    Code:
    #include <vector>
    #include <stdio.h>
    class myclass{
    int a;
    }
    using namespace std;
    
    int main()
    {
    
    myclass m;
    
    std::vector<myclass> v;
    m.a=10;
    v.push_back(m);
    m.a=20;
    v.push_back(m);
    vector (myclass) :: iterator it;
    myclass *s;
    for (it=v.begin();it<v.end;it++)
    {
       s=it;
       printf("a=%d\n",s->a)
    }
    
    }
    Last edited by Niheel; Oct 4 '10, 01:54 PM.
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    What is 'I can not' ? You don't know how to do it, program doesn't compile (link) with this and that error, program aborts at runtime with this and that message, something els? Not that I don't know about default access modifier for classes as opposed to structs, but....

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      There are a ton of coding errors in your sample.

      The problem is not with vector. It's with your not using correct C++ syntax.

      I suggest you compile the code and fix the first error. Then recompile and fix the next first error. Eventually, all of the errors will go away and I suspect you will have answered your own question.

      Comment

      • Oralloy
        Recognized Expert Contributor
        • Jun 2010
        • 988

        #4
        This page and its children will help you a lot.

        C++ Vectors

        Also, you need to run your code through a compiler before bringing questions here. What you have is utterly untested.

        Comment

        Working...