2 Replies - 542 Views - Last Post: 18 November 2011 - 05:32 PM Rate Topic: -----

#1 marioscs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-June 11

Iterator problem Don't know how to access object function on a vec

Posted 17 November 2011 - 04:39 PM

Hi guys i have a question,i have an excercice and im stuck.I have to access a print() function of an object stored in a vector.My teacher told us how to do that but only with vectors with integers and chars.Thanks for your help...
#include <iostream>
#include <vector>

using namespace std;

class Song{

    private:
        string title;
        string artist;
        int duration;

    public:

        //set_fucntions

        void setTitle(string _title)
        {
            title=_title;
        }

        void setArtist(string _artist)
        {
            artist=_artist;
        }

        void setDuration(int _duration)
        {
            duration=_duration;
        }

        //get_fucntions

        string getTitle()
        {
            return title;
        }

        string getArtist()
        {
            return artist;
        }

        int getDuration()
        {
            return duration;
        }

        //constructors

        Song()
        {



        }


        Song(string _title,string _artist,int _duration)
        {
            title=_title;
            artist=_artist;
            duration=_duration;
        }


        //print

        void print()
        {
            cout<< "Titlos Kommatioy:"<<title<<endl;
            cout<< "Onoma Kallitexni:" << artist <<endl;
            cout<< "Diarkeia Kommatioy:" << duration <<endl;
        }





        //tested perfectly working




};


class Album{


        private:
            string artist;
            string album_title;
            vector <Song> album_songs;


        public:


            void addSong(Song _song)
            {
                album_songs.push_back(_song);
            }



            void addSong(Song _song,int position)
            {
                album_songs.insert(album_songs.begin()+position,_song);
            }

            void deleteSong(int position)
            {
                album_songs.erase(album_songs.begin()+position);
            }

            void printAlbum()
            {
                vector<Song>::iterator n=album_songs.begin();


                for(n=album_songs.begin();n!=album_songs.end();n++)
                {
                    //how to access print() of album_songs;

                }

            }




















};




int main()
{


    return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Iterator problem Don't know how to access object function on a vec

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: Iterator problem Don't know how to access object function on a vec

Posted 17 November 2011 - 05:02 PM

Hint: you dereference the iterator to access the object to which it's pointing. The * operator is the dereference operator. Or -> would also work.

EDIT: Tutorial 1
Tutorial 2
Tutorial 3

This post has been edited by JackOfAllTrades: 17 November 2011 - 05:05 PM

Was This Post Helpful? 1
  • +
  • -

#3 marioscs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-June 11

Re: Iterator problem Don't know how to access object function on a vec

Posted 18 November 2011 - 05:32 PM

Thank you very much Jack...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1