473,385 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem with accessing objects of vectors

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
9 3883

"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
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
"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
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
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
> >> 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
ree
Thanks boths of you guys it worked.
Jul 19 '05 #8
ree
Thanks boths of you guys it worked.

Jul 19 '05 #9

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Achim Domma (Procoders) | last post by:
Hi, I have a problem with a DCOM server written in python. Here is my minimal test object: class TestObject: _reg_clsid_ = "{ECDBB3BC-F0BF-4eef-87C0-D179A928DAB5}" _reg_progid_ =...
4
by: Jaydeep | last post by:
Hello, I am facing a strange problem. Problem accessing remote database from ASP using COM+ server application having VB components (ActiveX DLL) installed. Tier 1 : ASP front End (IIS 5.0) Tire...
5
by: berthelot samuel | last post by:
Hi, I have a problem with structures that lose their content when I pass them to a function. Here are the structures typedef struct VERTEX { int id; /* id of this vertex */ float x,...
0
by: jwl | last post by:
I'm having a problem with a bit of code that I have "adopted". It was partially complete when I took it over. The function of the code is to read a log file, locate files described in that log...
9
by: mihai | last post by:
How can I build a set of unordered objects lets say POINT_2D objects? The POINT_2D class does not have a consistent operator<. Have a nice day, Mihai.
2
by: ree | last post by:
I am new to C# in C++ you can use vectors to handle a unknown quantity of set of objects. But in C# not sure what to use. As I tried arraylists but they can hold a variety of objects. So...
3
Digital Don
by: Digital Don | last post by:
I have a problem sending the Vectors as Referencial parameters..i.e. I am having a struct vector struct board { int r; int c; };
4
by: mathlete | last post by:
Hello, I'm just starting to learn C++, coming from a Java / bit of C background. I have some questions about how vectors of objects and pointers work in C++. Say I want to create a vector of...
2
by: Kurda Yon | last post by:
Hi, I just started to learn Python. I understood how one can create a class and define a method related with that class. According to my understanding every call of a methods is related with a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.