By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 2,122 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

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

P: 1
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


Expand|Select|Wrap|Line Numbers
  1. #include <vector>
  2. #include <stdio.h>
  3. class myclass{
  4. int a;
  5. }
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.  
  11. myclass m;
  12.  
  13. std::vector<myclass> v;
  14. m.a=10;
  15. v.push_back(m);
  16. m.a=20;
  17. v.push_back(m);
  18. vector (myclass) :: iterator it;
  19. myclass *s;
  20. for (it=v.begin();it<v.end;it++)
  21. {
  22.    s=it;
  23.    printf("a=%d\n",s->a)
  24. }
  25.  
  26. }
Oct 4 '10 #1
Share this Question
Share on Google+
3 Replies


100+
P: 687
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....
Oct 4 '10 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
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.
Oct 4 '10 #3

Oralloy
Expert 100+
P: 983
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.
Oct 4 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.