Problem with accessing objects of vectors



 DEVELOP > c-Plus-Plus > Problem with accessing objects of vectors

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "ree"
Date: 08 Nov 2003 08:48:02 PM
Object: Problem with accessing objects of vectors
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
.

User: "Dave"

Title: Re: Problem with accessing objects of vectors 08 Nov 2003 09:05:11 PM
"ree" <ree@hotmail.coom> 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[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
.
User: "ree"

Title: Re: Problem with accessing objects of vectors 08 Nov 2003 09:30:55 PM
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" <better_cs_now@yahoo.com> wrote in news:vqrblss9slahe0
@news.supernews.com:


"ree" <ree@hotmail.coom> 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[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



.
User: "Dave"

Title: Re: Problem with accessing objects of vectors 08 Nov 2003 09:50:23 PM
"ree" <ree@hotmail.coom> 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!
.
User: "ree"

Title: Re: Problem with accessing objects of vectors 08 Nov 2003 10:22:20 PM
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" <better_cs_now@yahoo.com> wrote in news:vqreah8lln8n31
@news.supernews.com:

"ree" <ree@hotmail.coom> 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!



.
User: "Ron Natalie"

Title: Re: Problem with accessing objects of vectors 09 Nov 2003 01:48:55 PM
"ree" <ree@hotmail.coom> 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
.

User: "Dave"

Title: Re: Problem with accessing objects of vectors 08 Nov 2003 10:34:28 PM

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();
.
User: "ree"

Title: Re: Problem with accessing objects of vectors 08 Nov 2003 10:41:27 PM
Thanks boths of you guys it worked.
.


User: "Artie Gold"

Title: Re: Problem with accessing objects of vectors 08 Nov 2003 10:33:12 PM
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" <better_cs_now@yahoo.com> wrote in news:vqreah8lln8n31
@news.supernews.com:


"ree" <ree@hotmail.coom> 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.
.
User: "ree"

Title: Re: Problem with accessing objects of vectors 08 Nov 2003 10:41:39 PM
Thanks boths of you guys it worked.
.







  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER