473,782 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3905

"ree" <re*@hotmail.co om> 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(newSemeste r);

HtmlWriter HtmlSemesterPag e;
HtmlSemesterPag e.htmlSemesterP age(*newSemeste r);

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

void HtmlWriter::htm lSemesterPage(S emester theSemester)
{
std::string subjectId = theSemester.the Subjects[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:vqrblss9sl ahe0
@news.supernews .com:

"ree" <re*@hotmail.co om> 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.co om> wrote in message
news:Xn******** *************** *****@211.29.13 3.50...
I am actually passing one of the semesters to a particular function.
---
Semester * newSemester = new Semester(name, intYear, season,rSem);
semesters.push_ back(newSemeste r);

HtmlWriter HtmlSemesterPag e;
HtmlSemesterPag e.htmlSemesterP age(*newSemeste r);

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

void HtmlWriter::htm lSemesterPage(S emester theSemester)
{
std::string subjectId = theSemester.the Subjects[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 "SubjectDat a.h"

class Semester;
class SubjectData;

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

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

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

void downloadSubject Data();
void loadSubjectData ();

SubjectData theSubjectData[7];
private:
std::string subjectName;
int subjectNumber;
std::string courseId;
bool monitor;

};



"Dave" <be***********@ yahoo.com> wrote in news:vqreah8lln 8n31
@news.supernews .com:
"ree" <re*@hotmail.co om> wrote in message
news:Xn******** *************** *****@211.29.13 3.50...
I am actually passing one of the semesters to a particular function.
---
Semester * newSemester = new Semester(name, intYear, season,rSem);
semesters.push_ back(newSemeste r);

HtmlWriter HtmlSemesterPag e;
HtmlSemesterPag e.htmlSemesterP age(*newSemeste r);

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

void HtmlWriter::htm lSemesterPage(S emester theSemester)
{
std::string subjectId = theSemester.the Subjects[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 "SubjectDat a.h"

class Semester;
class SubjectData;

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

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

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

void downloadSubject Data();
void loadSubjectData ();

SubjectData theSubjectData[7];
private:
std::string subjectName;
int subjectNumber;
std::string courseId;
bool monitor;

};



"Dave" <be***********@ yahoo.com> wrote in news:vqreah8lln 8n31
@news.supernews .com:

"ree" <re*@hotmail.co om> 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.pu sh_back(newSeme ster);

HtmlWriter HtmlSemesterPag e;
HtmlSemester Page.htmlSemest erPage(*newSeme ster);

---
In that function i am trying to read the courseId string from the

first
subject in the semester.

void HtmlWriter::htm lSemesterPage(S emester theSemester)
{
std::strin g subjectId = theSemester.the Subjects[0].getCourseId();
Since theSemester.the Subjects[0] is of type std::vector<Sub ject *>,
the above line should be:

std::string subjectId = theSemester.the Subjects[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::htm lSemesterPage(S emester theSemester)
{
std::string subjectId = theSemester.the Subjects[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.the Subjects[0] is a pointer. So, you need to use "->" rather
than "." after it. See below:

std::string subjectId = theSemester.the Subjects[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.co om> wrote in message news:Xn******** *************** *****@211.29.13 3.50...
vector <Subject *> theSubjects;

This is a vector of pointers.
Semester * newSemester = new Semester(name, intYear, season,rSem);
semesters.push_ back(newSemeste r);
Pushing a poitner, fine.
std::string subjectId = theSemester.the Subjects[0].getCourseId();


theSemebers.the Subjects[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.the Subjects[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
1465
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_ = "DComTest.Object" _reg_desc_ = "DComTest.Object" _public_methods_ =
4
2988
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 2 : COM objects Tier 3 : SQL Server 2000 DB Flow is : ASP talks with COM objects and COM objects talks with DB Scenario 1
5
1259
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, y, z; /* vertex coordinates */
0
1233
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 file and fill in a few tables in a couple of MySql databases. The program is using a number of std objects, hashmaps, linklists, vectors etc. It makes direct calls to the MySql api. The version of MySql is 3.23.58. Redhat 9, g++ (GCC) 3.2.2...
9
1679
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
3152
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 when I try to use functions that go through (eg. for loop) of group objects (eg. Cars) and get their (attributes eg. for Class Car attribute Year of Make ) I am having compilation errors.
3
4315
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
26950
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 SomeClass objects, and the SomeClass constructor takes one argument. However, SomeClass is not copyable, so I can't do something like: vector<SomeClass> someVector; for (int i=0; i<args.size(); i++){ someVector.push_back( SomeClass(args) ) }
2
1105
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 specific object. For example, we have a method "length", than we call this method as the following "x.length()" or "y.length()" or "z.length()", where z, y, and z are objects of the class. I am wandering if it is possible to create a method...
0
9479
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10311
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10146
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9942
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8967
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.