Results 1 to 3 of 3

Thread: c++, cout and vectors of objects

  1. #1
    Join Date
    Dec 2007
    Location
    Idaho
    Beans
    4,976
    Distro
    Ubuntu 20.04 Focal Fossa

    c++, cout and vectors of objects

    So I'm thoroughly confused I'm trying to print elements of a vector of objects, and I get the comiler message of
    error: no match for ‘operator<<’ in ‘std:perator<< <std::char_traits<char> >((* & std::cout.std::basic_ostream<_CharT, _Traits>:perator<< <char, std::char_traits<char> >((i + 1ul))), ((const char*)". ")) << ((GradeBook*)this)->GradeBook::theClass.std::vector<_Tp, _Alloc>:perator[]<Student, std::allocator<Student> >(i).Student::getName’|
    The code:
    Code:
        for ( size_t i = 0; i < theClass.size(); ++i ) {
            cout << i + 1 << ". " << theClass[i].getName << endl;
            }
    theClass's definition
    Code:
    vector<Student> theClass;
    The student header
    Code:
     class Student
     {
    	private:
    		string studentName;
    	public:
    	    /* Constructs a Student object with student name studentName
    		 * @param studentName student name
    		 */
    		Student( string );
    
    		/* Set student name to name
    		 * @param name student Name
    		 */
    		void setName( string studentName ) { this->studentName = studentName; }
    
    		/* returns a studentName
    		 * @return student name
    		 */
    		string getName() const { return studentName; }
    };
    "You can't expect to hold supreme executive power just because some watery tart lobbed a sword at you"

    "Don't let your mind wander -- it's too little to be let out alone."

  2. #2
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,588
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: c++, cout and vectors of objects

    I *think* it is simply that you are missing the parentheses for calling the getName method. i.e. you want:
    Code:
        cout << i + 1 << ". " << theClass[i].getName() << endl;

  3. #3
    Join Date
    Dec 2007
    Location
    Idaho
    Beans
    4,976
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: c++, cout and vectors of objects

    oh gosh, it's always the simple things. That did eliminate the compiler error. Thanks.

    ::feeling sheepish::
    Last edited by jerome1232; December 5th, 2013 at 12:10 AM.
    "You can't expect to hold supreme executive power just because some watery tart lobbed a sword at you"

    "Don't let your mind wander -- it's too little to be let out alone."

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •