want to initialize pointer dynamically using map

this is my header file ITECH7603Class.h...

#ifndef ITECH7603CLASS_H
#define ITECH7603CLASS_H
#include "Student.h"
#include <set>
#include <map>
#include <fstream>

class ITECH7603Class{
public:
ITECH7603Class();
ITECH7603Class(int dummy);
ITECH7603Class(set<Student>*);
~ITECH7603Class();
void addStudent(Student*);
map<string,Student>* getGroup();
void save();
private:
map<string,Student>* group;
};
#endif


and i want to do followig things i didn't get them....can u pls help me to do that

(1) Constructor ITECH7603Class() and the destructor:
A default constructor initializes dynamically the group field.
The destructor deletes the group pointer.


(2) Constructor ITECH7603Class(int dummy):
This constructor does not require any parameter. However, to distinguish it from the default constructor you should use some dummy argument (say of int type). This constructor opens the “ITECH7603Students.bin” file, if such file exists, and reads information from the file into the (*group) map using addStudent() function. If the file “ITECH7603Students.bin” does not exist, the constructor behaves as the default constructor.


(3) Constructor ITECH7603Class(set<Student>* students):
This constructor takes a set of Student objects (in fact, a pointer to the set) as an argument and adds students from the set to the (*group) map, i.e. initializes the group field.
Last edited on
We can't provide answers to past exam questions. You'll need to make an effort yourself, then we can assist.
hey guys....I got your suggesion....I tried a lot....man....but I haven't done pointer any where...in my whole study.....that's y I am asking..this quesion is not just i asked u...there are so manu other things I did my self...this is just bit part of it...If it will be solved I can go ahead...that's y guys.....

I am sorry abt asking u same question again.....

Pls help me in this case guys.....

Thanking u for your concern...(whether u reply or not..)
thanx buddy...
this is my header file Student.h

#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class Student{
public:
Student();
Student(string name,string surname,int a1, int a2, int test, int exam);

string getName()const;
string getSurname()const;
int getAssignment1Mark()const;
int getAssignment2Mark()const;
int getLabTestMark()const;
int getExamMark()const;

void setAssignment1Mark(int);
void setAssignment2Mark(int);
void setLabTestMark(int);
void setExamMark(int);

bool passed()const;
string getGrade()const;
friend ostream& operator<<(ostream& stream, const Student &);
bool operator<(const Student &)const;

private:
string name;
string surname;
int assignment1Mark;
int assignment2Mark;
int labTestMark;
int examMark;
};
#endif


I have created this function

void save()
{
ITECH7603Class::getGroup()::iterator s;
fstream myfile;
{
myfile.open("ITECH7603Students.bin",ios::binary);

for(s=ITECH7603Class::group.begin(); s!=ITECH7603Class::group.end(); s++)
{
myfile.write(reinterpret_cast<char*>(s), sizeof(Student));


}
myfile.close();

}

}


it gave me following error..

In function `void save()':

error: cannot call member function ITECH7603Class::getGroup()' without object|
`std::map<std::string, Student, std::less<std::string>, std::allocator<std::pair<const std::string, Student> > >*


Can u help me to solve this error...pls
It helps if you format your code using the Code tags.

There are a couple of problems.

The first is your iterator declaration and how it's used. I'll repost a subset of your code.
1
2
3
4
5
6
7
8
9
10
class ITECH7603Class
{
public:
	//...
	std::map<std::string,Student>* getGroup();
	void save();

private:
	std::map<std::string,Student>* group;
};


The save function should be ITECH7603Class::save rather than a global save function. It might be better to pass a stream to save onto rather than doing the file stuff in the save function. More on that later.

The syntax for iterating the map would be something like:
1
2
3
    for (std::map<std::string, Student>::iterator iter = group->begin(); iter != group->end(); ++iter)
    {
    }


I also think that making group a pointer to a map is unnecessary it this instance.


Next is serialisation. If you want to save a collection of things, you should ask each element to save itself. The save function you posted attempts to save the elements itself. It can't do it properly because the data in the objects is private (as it should be). If you added the ability to read/write Students on streams, your life would be much simpler. You already have half of it.
1
2
3
4
5
6
7
8
9
std::ostream& Student::operator<<(std::ostream &os, const Student &s)
{
    os << s.name << std::endl;
    os << s.surname << std::endl;
    os << s.assignment1Mark << ' ';
    os << s.assignment2Mark << ' ';
    os << s.labTestMark << ' '
    os << s.examMark << std::endl;
}



Back to the save function. It might be better to write code like:
1
2
3
4
5
6
7
int main()
{
    ITECH7603Class students;
    // ...
    std::ofstream os("Students.bin");
    students.save(os);
}



So putting it all together, your save function looks like:
1
2
3
4
5
6
7
8
void ITECH7603Class::save(std::ostream &os)
{
    // if we declare group as a map rather than a pointer to a map
    for (std::map<std::string, Student>::iterator iter = group.begin(); iter != group.end(); ++iter)
    {
        os << *iter;
    }
}


I hope that helps.
Do I need to initialize group filed any where else in my code..?

I have changed the save function like below:

void ITECH7603Class::save(std::ostream &os)

{
std::map<std::string, Student>::iterator iter;

for (iter = group.begin(); iter != group.end(); ++iter)
{
os << *iter;
}
}


Note : I have to use pointer to map cause that's description in my assignment...so what would b the other way to solve this problem using pointer to map.


but it gives me error like

"error: request for member `begin' in `((ITECH7603Class*)this)->ITECH7603Class::group', which is of non-class type `std::map<std::string, Student, std::less<std::string>, std::allocator<std::pair<const std::string, Student> > >*'"

and gives same error for 'end' as well..
Last edited on
If group is declared as a pointer, the loop is:
1
2
3
4
5
6
7
8
void ITECH7603Class::save(std::ostream &os)
{
    std::map<std::string, Student>::iterator iter;
    for (iter = group->begin(); iter != group->end(); ++iter)
    {
        os << *iter;
    }
}
Thank u mate...!!!!

It is really helpfulll....!!!

Thanx for all your concern....
Topic archived. No new replies allowed.