By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,147 Members | 1,402 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,147 IT Pros & Developers. It's quick & easy.

Problem with accessing objects of vectors

P: n/a
ree
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
Jul 19 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a

"ree" <re*@hotmail.coom> wrote in message
news:Xn***************************@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[i] gives you semester i
Semesters[i].Subjects gives you the subjects for semester i
Semesters[i].Subjects[j] gives you subject j for semester i
Semesters[i].Subjects[j].name( ) gives you the name of subject j for
semester i

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

HTH,
Dave
Jul 19 '05 #2

P: n/a
ree
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" <be***********@yahoo.com> wrote in news:vqrblss9slahe0
@news.supernews.com:

"ree" <re*@hotmail.coom> wrote in message
news:Xn***************************@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[i] gives you semester i
Semesters[i].Subjects gives you the subjects for semester i
Semesters[i].Subjects[j] gives you subject j for semester i
Semesters[i].Subjects[j].name( ) gives you the name of subject j for
semester i

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

HTH,
Dave


Jul 19 '05 #3

P: n/a
"ree" <re*@hotmail.coom> wrote in message
news:Xn****************************@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!
Jul 19 '05 #4

P: n/a
ree
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" <be***********@yahoo.com> wrote in news:vqreah8lln8n31
@news.supernews.com:
"ree" <re*@hotmail.coom> wrote in message
news:Xn****************************@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!


Jul 19 '05 #5

P: n/a
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" <be***********@yahoo.com> wrote in news:vqreah8lln8n31
@news.supernews.com:

"ree" <re*@hotmail.coom> wrote in message
news:Xn****************************@211.29.133.5 0...
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.

Jul 19 '05 #6

P: n/a
> >> 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();

Jul 19 '05 #7

P: n/a
ree
Thanks boths of you guys it worked.
Jul 19 '05 #8

P: n/a
ree
Thanks boths of you guys it worked.

Jul 19 '05 #9

P: n/a

"ree" <re*@hotmail.coom> wrote in message news:Xn****************************@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
Jul 19 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.