Thread: Trying to print toString from object held in vector

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    12

    Trying to print toString from object held in vector

    Apologies in advance if this is a really dumb question, but I'm pretty new at C++. Anyway, I have a Student object that has the following toString:

    Code:
    string Student::toString() const {
        return studentName + "\t" + studentProgramCode;
    }
    Various Student objects are stored in a vector (within StudentRecord.cpp) defined as this:

    Code:
    vector<Student> students;
    I am currently trying to print out all of the Student objects in the vector, using their toString. However, I'm rather stuck. I so far have this:

    Code:
    void StudentRecord::print() const {
         int i;
         for(i=0; i < students.size(); i++) {
             cout << students[i] << endl;
         }
    }
    I know the students[i] part is the bit that's wrong, but I can't work out what to put there. Thanks in advance for your help.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    students[i].toString()
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    12
    Thank you! I knew it was a silly question.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by whiskedaway View Post
    Thank you! I knew it was a silly question.
    You may think about how to modify your Student class in a way that the original StudentRecord::print() function will work...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems defining classes
    By esmeco in forum C++ Programming
    Replies: 47
    Last Post: 10-24-2007, 01:13 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. How can I define and pass a pointer to a vector object?
    By asmileguo in forum C++ Programming
    Replies: 3
    Last Post: 09-07-2006, 11:19 AM
  4. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  5. Reverse Print Vector Array help
    By guda in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2002, 06:47 PM