Can't seem to access the Struct within my vector

This is a discussion on Can't seem to access the Struct within my vector within the C++ Programming forums, part of the General Programming Boards category; Hello, so I am trying to create a small mini program that iterates through a vector and outputs something according ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    110

    Can't seem to access the Struct within my vector

    Hello, so I am trying to create a small mini program that iterates through a vector and outputs something according to a book. I have no problem understanding the problem, but I came across a problem in which the iterator doesn't access the struct inside the vector. I can't seem to figure out why thought. Logically dereferencing the iterator should give me the struct, then accessing the fields inside the struct should be simple, "(*iter).FIELD". I could be wrong though...

    This is the error
    Code:
    error C2039: 'name' : is not a member of 'std::vector<_Ty>'
    This is my code
    Code:
    int main(){
    	typedef vector<Student_info> students;
    
    	students studs;
    	Student_info record;
    	string::size_type maxlen = 0;
    
    	while(read(cin, record)){
    
    		maxlen = max(maxlen, record.name.size());
    		studs.push_back(record);
    	}
    
    	sort(studs.begin(), studs.end(), compare);
    
    	for(students::iterator i = studs.begin(); i != studs.end(); ++i){
    
    		cout << (*studs).name << string(maxlen + 1 - (*studs).name.size(), ' ');
    
    		try{
    			double final_grade = grade(*studs);
    			streamsize prec = cout.precision();
    			cout << setprecision(3) << final_grade << setprecision(prec);
    		}
    
    		catch(domain_error e){
    			cout << e.what();
    		}
    		cout << endl
    	}
    	return 0;
    }
    This is my struct that I have located in another header file.
    Code:
    typedef std::vector<double> homework;
    
    struct Student_info{
    	std::string name;
    	double midterm, final;
    	homework hw;
    };

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    147
    Your iterator is i, but you're using studs as if IT were the iterator.

    Also, although you can use (*i).name

    it's usually done

    i->name

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by dnguyen1022 View Post
    Logically dereferencing the iterator should give me the struct, then accessing the fields inside the struct should be simple, "(*iter).FIELD". I could be wrong though...
    It should, but unfortunately, your code isn't using the iterator, it's using the vector:
    Code:
    cout << (*studs).name << string(maxlen + 1 - (*studs).name.size(), ' ');
    Replace studs with i on that line.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Vector Addition Using OOP?
    By everydaybh in forum C++ Programming
    Replies: 3
    Last Post: 04-09-2009, 05:09 AM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 07:18 AM
  4. vector of pointers
    By gamer4life687 in forum C++ Programming
    Replies: 3
    Last Post: 09-26-2005, 10:49 PM
  5. Collision detection algorithm
    By Hannwaas in forum Game Programming
    Replies: 5
    Last Post: 11-30-2001, 12:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21