Problem with accessing objects of vectors

R

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
 
D

Dave

ree said:
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
 
R

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




ree said:
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
 
D

Dave

ree said:
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!
 
R

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;

};









ree said:
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!
 
A

Artie Gold

ree said:
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;

};









@news.supernews.com:

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);
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();

HTH,
--ag
 
D

Dave

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();
 
R

Ron Natalie

ree said:
vector <Subject *> theSubjects;
This is a vector of pointers.

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top