Problem with accessing objects of vectors

Discussion in 'C++' started by ree, Nov 9, 2003.

  1. ree

    ree Guest

    Unlike arrays I am having problems accessing objects placed in a vector

    I have a vector of objects called Semesters

    Each Semester has its own vector of Subjects.

    I am having problems accssing attributes of the a subject given a
    particular semester.

    Can someone help me out with the syntax
    ree, Nov 9, 2003
    #1
    1. Advertising

  2. ree

    Dave Guest

    "ree" <> wrote in message
    news:Xns942E8D4CDE87reehotmailcoom@211.29.133.50...
    > Unlike arrays I am having problems accessing objects placed in a vector
    >
    > I have a vector of objects called Semesters
    >
    > Each Semester has its own vector of Subjects.
    >
    > I am having problems accssing attributes of the a subject given a
    > particular semester.
    >
    > Can someone help me out with the syntax


    It might help if you post some code, but here's a stab at it:

    Semesters gives you semester i
    Semesters.Subjects gives you the subjects for semester i
    Semesters.Subjects[j] gives you subject j for semester i
    Semesters.Subjects[j].name( ) gives you the name of subject j for
    semester i

    (Assuming your subject class has a name() method)

    HTH,
    Dave
    Dave, Nov 9, 2003
    #2
    1. Advertising

  3. ree

    ree Guest

    I am actually passing one of the semesters to a particular function.


    ---
    Semester * newSemester = new Semester(name, intYear, season,rSem);
    semesters.push_back(newSemester);

    HtmlWriter HtmlSemesterPage;
    HtmlSemesterPage.htmlSemesterPage(*newSemester);

    ---
    In that function i am trying to read the courseId string from the first
    subject in the semester.

    void HtmlWriter::htmlSemesterPage(Semester theSemester)
    {
    std::string subjectId = theSemester.theSubjects[0].getCourseId();


    but I keep getting this error...
    error C2228: left of '.getCourseId' must have class/struct/union type




    "Dave" <> wrote in news:vqrblss9slahe0
    @news.supernews.com:

    >
    > "ree" <> wrote in message
    > news:Xns942E8D4CDE87reehotmailcoom@211.29.133.50...
    >> Unlike arrays I am having problems accessing objects placed in a

    vector
    >>
    >> I have a vector of objects called Semesters
    >>
    >> Each Semester has its own vector of Subjects.
    >>
    >> I am having problems accssing attributes of the a subject given a
    >> particular semester.
    >>
    >> Can someone help me out with the syntax

    >
    > It might help if you post some code, but here's a stab at it:
    >
    > Semesters gives you semester i
    > Semesters.Subjects gives you the subjects for semester i
    > Semesters.Subjects[j] gives you subject j for semester i
    > Semesters.Subjects[j].name( ) gives you the name of subject j for
    > semester i
    >
    > (Assuming your subject class has a name() method)
    >
    > HTH,
    > Dave
    >
    >
    >
    ree, Nov 9, 2003
    #3
  4. ree

    Dave Guest

    "ree" <> wrote in message
    news:Xns942E9492D9506reehotmailcoom@211.29.133.50...
    > I am actually passing one of the semesters to a particular function.
    >
    >
    > ---
    > Semester * newSemester = new Semester(name, intYear, season,rSem);
    > semesters.push_back(newSemester);
    >
    > HtmlWriter HtmlSemesterPage;
    > HtmlSemesterPage.htmlSemesterPage(*newSemester);
    >
    > ---
    > In that function i am trying to read the courseId string from the first
    > subject in the semester.
    >
    > void HtmlWriter::htmlSemesterPage(Semester theSemester)
    > {
    > std::string subjectId = theSemester.theSubjects[0].getCourseId();
    >
    >
    > but I keep getting this error...
    > error C2228: left of '.getCourseId' must have class/struct/union type


    Could you please show your class definitions for Semester and Subject?
    Thanks!
    Dave, Nov 9, 2003
    #4
  5. ree

    ree Guest

    Semester.h
    -------
    #include <vector>
    #include <iostream>
    #include <fstream>
    #include <string>

    #include "Subject.h"


    using namespace std;

    class User;
    class Subject;

    class Semester
    {
    public:
    Semester( std::string, int , std::string, int);
    ~Semester ();

    std::string getsemesterName() const;

    void loadSubjects();

    void addSubject(std::string, std::string);
    vector <Subject *> theSubjects;

    private:
    std::string semesterName;
    bool currentSemester;
    int year;
    std::string season;
    };


    -------

    Subject.h

    #include <iostream>
    #include <fstream>
    #include <string>

    #include "SubjectData.h"

    class Semester;
    class SubjectData;

    class Subject
    {
    public:
    Subject ();
    Subject(std::string, std::string);

    ~Subject ();
    std::string getSubjectName() const;
    std::string getCourseId() const;

    void setSubjectName(std::string name);
    void setCourseId(std::string id);

    void downloadSubjectData();
    void loadSubjectData();

    SubjectData theSubjectData[7];


    private:
    std::string subjectName;
    int subjectNumber;
    std::string courseId;
    bool monitor;

    };









    "Dave" <> wrote in news:vqreah8lln8n31
    @news.supernews.com:

    > "ree" <> wrote in message
    > news:Xns942E9492D9506reehotmailcoom@211.29.133.50...
    >> I am actually passing one of the semesters to a particular function.
    >>
    >>
    >> ---
    >> Semester * newSemester = new Semester(name, intYear, season,rSem);
    >> semesters.push_back(newSemester);
    >>
    >> HtmlWriter HtmlSemesterPage;
    >> HtmlSemesterPage.htmlSemesterPage(*newSemester);
    >>
    >> ---
    >> In that function i am trying to read the courseId string from the

    first
    >> subject in the semester.
    >>
    >> void HtmlWriter::htmlSemesterPage(Semester theSemester)
    >> {
    >> std::string subjectId = theSemester.theSubjects[0].getCourseId();
    >>
    >>
    >> but I keep getting this error...
    >> error C2228: left of '.getCourseId' must have class/struct/union

    type
    >
    > Could you please show your class definitions for Semester and Subject?
    > Thanks!
    >
    >
    >
    ree, Nov 9, 2003
    #5
  6. ree

    Artie Gold Guest

    ree wrote:
    > Semester.h
    > -------
    > #include <vector>
    > #include <iostream>
    > #include <fstream> #include <string>
    >
    > #include "Subject.h"
    >
    >
    > using namespace std;
    >
    > class User;
    > class Subject;
    >
    > class Semester
    > {
    > public:
    > Semester( std::string, int , std::string, int);
    > ~Semester ();
    >
    > std::string getsemesterName() const;
    >
    > void loadSubjects();
    >
    > void addSubject(std::string, std::string);
    > vector <Subject *> theSubjects;
    >
    > private:
    > std::string semesterName;
    > bool currentSemester;
    > int year;
    > std::string season;
    > };
    >
    >
    > -------
    >
    > Subject.h
    >
    > #include <iostream>
    > #include <fstream> #include <string>
    >
    > #include "SubjectData.h"
    >
    > class Semester;
    > class SubjectData;
    >
    > class Subject
    > {
    > public:
    > Subject ();
    > Subject(std::string, std::string);
    >
    > ~Subject ();
    > std::string getSubjectName() const;
    > std::string getCourseId() const;
    >
    > void setSubjectName(std::string name);
    > void setCourseId(std::string id);
    >
    > void downloadSubjectData();
    > void loadSubjectData();
    >
    > SubjectData theSubjectData[7];
    >
    >
    > private:
    > std::string subjectName;
    > int subjectNumber;
    > std::string courseId;
    > bool monitor;
    >
    > };
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > "Dave" <> wrote in news:vqreah8lln8n31
    > @news.supernews.com:
    >
    >
    >>"ree" <> wrote in message
    >>news:Xns942E9492D9506reehotmailcoom@211.29.133.50...
    >>
    >>>I am actually passing one of the semesters to a particular function.
    >>>
    >>>
    >>>---
    >>>Semester * newSemester = new Semester(name, intYear, season,rSem);
    >>>semesters.push_back(newSemester);
    >>>
    >>>HtmlWriter HtmlSemesterPage;
    >>>HtmlSemesterPage.htmlSemesterPage(*newSemester);
    >>>
    >>>---
    >>>In that function i am trying to read the courseId string from the

    >>

    > first
    >
    >>>subject in the semester.
    >>>
    >>>void HtmlWriter::htmlSemesterPage(Semester theSemester)
    >>>{
    >>>std::string subjectId = theSemester.theSubjects[0].getCourseId();


    Since theSemester.theSubjects[0] is of type std::vector<Subject *>,
    the above line should be:

    std::string subjectId = theSemester.theSubjects[0]->getCourseId();

    >>>
    >>>
    >>>but I keep getting this error...
    >>> error C2228: left of '.getCourseId' must have class/struct/union

    >>

    > type
    >
    >>Could you please show your class definitions for Semester and Subject?


    HTH,
    --ag


    --
    Artie Gold -- Austin, Texas
    Oh, for the good old days of regular old SPAM.
    Artie Gold, Nov 9, 2003
    #6
  7. ree

    Dave Guest

    > >> void HtmlWriter::htmlSemesterPage(Semester theSemester)
    > >> {
    > >> std::string subjectId = theSemester.theSubjects[0].getCourseId();
    > >>
    > >>
    > >> but I keep getting this error...
    > >> error C2228: left of '.getCourseId' must have class/struct/union

    > type
    > >
    > > Could you please show your class definitions for Semester and Subject?
    > > Thanks!


    theSemester.theSubjects[0] is a pointer. So, you need to use "->" rather
    than "." after it. See below:

    std::string subjectId = theSemester.theSubjects[0]->getCourseId();
    Dave, Nov 9, 2003
    #7
  8. ree

    ree Guest

    Thanks boths of you guys it worked.
    ree, Nov 9, 2003
    #8
  9. ree

    ree Guest

    Thanks boths of you guys it worked.
    ree, Nov 9, 2003
    #9
  10. ree

    Ron Natalie Guest

    "ree" <> wrote in message news:Xns942E9D4A92567reehotmailcoom@211.29.133.50...

    > vector <Subject *> theSubjects;
    >

    This is a vector of pointers.

    > >> Semester * newSemester = new Semester(name, intYear, season,rSem);
    > >> semesters.push_back(newSemester);


    Pushing a poitner, fine.

    > >> std::string subjectId = theSemester.theSubjects[0].getCourseId();
    > >>


    theSemebers.theSubjects[0] has type Subject*. You can't apply the . operator
    to that. As the compiler told you, it needs to be a class type to do that.

    You wanted to write:
    std::string subjectId = theSemester.theSubjects[0]->getCourseID();

    As
    Ron Natalie, Nov 9, 2003
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bigbinc
    Replies:
    3
    Views:
    397
    Michael Borgwardt
    Nov 18, 2003
  2. Richard Anderson

    sorting vectors with Date objects

    Richard Anderson, Dec 16, 2003, in forum: Java
    Replies:
    7
    Views:
    1,020
    John C. Bollinger
    Dec 16, 2003
  3. Taria
    Replies:
    7
    Views:
    682
    Patricia Shanahan
    Oct 14, 2007
  4. Replies:
    3
    Views:
    691
    Shadowman
    Mar 26, 2008
  5. Guest
    Replies:
    0
    Views:
    438
    Guest
    Sep 14, 2005
Loading...

Share This Page