C++ iterator as class member used in class method - c++

i have the following code.
The for-loop at the end should go through the object of CCarList class, print out the the a_rz and vin of Car structure and stop when the AtEnd() method returns true.
But it doesnt stop and moreover when i try to reach the values of a_rz and vin it gives segmentation fault.
Could someone, please, explain how to use properly iterator in my CCarList class?
Thanks
typedef struct Car {
string a_rz;
unsigned int vin;
}Car;
class CCarList
{
public:
string RZ ( void ) const;
unsigned int VIN ( void ) const;
bool AtEnd ( void ) const;
void Next ( void );
vector<Car*> vCar;
vector<Car*>::const_iterator it = vCar.begin();
public:
CCarList ( void ){}
~CCarList ( void ){}
};
string CCarList::RZ ( void ) const {
return "ahoj"; //(**it).a_rz;
}
unsigned int CCarList::VIN ( void ) const{
return 5; //(**it).vin;
}
bool CCarList::AtEnd ( void ) const {
if(it == vCar.end()) return true;
return false;
}
void CCarList::Next ( void ){
it++;
}
int main() {
Car *a, *b, *c;
a = new Car;
b = new Car;
c = new Car;
(*a).a_rz = "abc";
(*a).vin = 45;
(*b).a_rz = "dfg";
(*b).vin = 65;
(*c).a_rz = "jkl";
(*c).vin = 23;
CCarList list_of_cars;
list_of_cars.vCar.push_back(a);
list_of_cars.vCar.push_back(b);
list_of_cars.vCar.push_back(c);
for ( ; ! list_of_cars . AtEnd (); list_of_cars . Next () )
cout << list_of_cars . RZ () << ", " << list_of_cars . VIN () << endl;
return 0;
}

Your problem is that iterator it is not being updated/invalidated after each push_back. After last insertion it still points to "nothing" as it was from beginning.
Soultion is simple -- update your iterator. Add a method for adding new elements:
void CCarList::Add(Car* car)
{
vCar.push_back(car);
it = vCar.begin();
}
and then just:
list_of_cars.Add(a);
list_of_cars.Add(b);
list_of_cars.Add(c);
Also regarding above problem, you're trying to wrap vector and provide same functionality that vectoralready provides. Consider moving functionality related to Car structure inside that structure. And leaving in CCarList only methods that are related to CCarList. Just a short piece of code to show you what I mean:
typedef struct Car {
string a_rz;
unsigned int vin;
} Car;
class CCarList {
public:
vector<Car*> vCar;
CCarList(void){}
~CCarList(void){}
};
int main() {
Car *a, *b, *c;
a = new Car;
b = new Car;
c = new Car;
a->a_rz = "abc";
a->vin = 45;
b->a_rz = "dfg";
a->vin = 65;
c->a_rz = "jkl";
c->vin = 23;
CCarList list_of_cars;
list_of_cars.vCar.push_back(a);
list_of_cars.vCar.push_back(b);
list_of_cars.vCar.push_back(c);
for(auto car : list_of_cars.vCar)
cout << car->a_rz << ", " << car->vin << endl;
return 0;
}

Related

Why doesn't the pointer write the address of the class?

I have an abstract class "Mark" and it has a child class "Int_num". I also have a "Subject" class. I want a pointer to the address in the memory of the "Mark" class to be written to the "mark" parameter when calling its constructor. What should I do to make the mark pointer point to the "Mark" class?" occurred, after the compiler complaint about "expression must have class type" or something like that in mark.print_mark()?
class Mark {
private:
int mark;
public:
virtual void change_mark(int);
virtual void print_mark();
virtual int return_mark();
};
class Int_mark : public Mark {
private:
int mark;
public:
Int_mark();
Int_mark(int);
~Int_mark();
void change_mark(int = 0);
void print_mark() const;
int return_mark() const;
};
Int_mark::Int_mark() {
std::string str_mark;
std::cout << "New mark: ";
std::cin.ignore();
std::getline(std::cin, str_mark);
str_mark = ltrim(rtrim(str_mark));
int new_mark;
try {
new_mark = stoi(str_mark);
} catch(...) {
std::cout <<"wq";
mark = 1;
return ;
}
try {
if((new_mark < 1) || (new_mark > 5))
throw 1;
else
mark = new_mark;
} catch(int a) {
std::cout << "qw" << std::endl;
mark = 1;
}
}
void Int_mark::print_mark() const {
std::cout << "Mark: " << mark << std::endl;
}
Subject
#include "Mark.h"
#include <string>
#include <vector>
class Subject {
private:
std::string name_subject;
std::string type_subject;
unsigned hour_subject = 0;
void *mark = nullptr;
public:
Subject();
Subject(std::string, int);
Subject(std::string, bool);
~Subject();
void change_mark(unsigned);
void change_mark(bool);
void rename_subj(std::string);
void add_hour(unsigned);
};
Subject::Subject() {
std::string name_sub;
std::cout << "Введите название предмета: ";
getline(std::cin, name_sub);
name_sub = split_string(name_sub);
name_subject = name_sub;
int select = 2;
if(select == 1) {
type_subject = "Bool";
//mark = new Bool_mark();
} else {
type_subject = "Int";
mark = new Int_mark();
//What should I do to make the mark pointer point to the "Mark" class?
mark.print_mark();
}
}
main
#include "subject/Subject.h"
using namespace std;
int main() {
Subject q;
}
What am I doing wrong? How should I do this?
The pointer mark is of type void *. You could cast it with
static_cast<Int_mark*>(mark)
and call the function with
static_cast<Int_mark*>(mark)->print_mark();
But usually in OOP mark would be a pointer to the base class
Mark *mark = nullptr;
Now you can check for errors with
mark = new Int_mark();
auto *m = dynamic_cast<Int_mark*>(mark);
if (m)
m->print_mark();
Remember the virtual destructor in the base class
virtual ~Mark();
When to use virtual destructors?
Here is a fixed version of your code:
#include <iostream>
#include <string>
#include <vector>
class Mark {
public:
virtual ~Mark() = default;
//virtual void change_mark(int) = 0;
virtual void print_mark() const = 0;
//virtual int return_mark() const = 0;
};
class Int_mark : public Mark {
private:
int mark;
public:
Int_mark();
Int_mark(int);
~Int_mark() override = default;
//void change_mark(int = 0) override;
void print_mark() const override;
//int return_mark() const override;
};
Int_mark::Int_mark() {
std::string str_mark;
std::cout << "New mark: ";
std::cin.ignore();
std::getline(std::cin, str_mark);
//str_mark = ltrim(rtrim(str_mark));
int new_mark;
try {
new_mark = stoi(str_mark);
} catch(...) {
std::cout <<"wq";
mark = 1;
return ;
}
try {
if((new_mark < 1) || (new_mark > 5))
throw 1;
else
mark = new_mark;
} catch(int a) {
std::cout << "qw" << std::endl;
mark = 1;
}
}
void Int_mark::print_mark() const {
std::cout << "Mark: " << mark << std::endl;
}
class Subject {
private:
std::string name_subject;
std::string type_subject;
unsigned hour_subject = 0;
Mark *mark = nullptr;
public:
Subject();
Subject(std::string, int);
Subject(std::string, bool);
~Subject();
void change_mark(unsigned);
void change_mark(bool);
void rename_subj(std::string);
void add_hour(unsigned);
};
Subject::Subject() {
std::string name_sub;
std::cout << "Введите название предмета: ";
getline(std::cin, name_sub);
//name_sub = split_string(name_sub);
name_subject = name_sub;
int select = 2;
if(select == 1) {
type_subject = "Bool";
//mark = new Bool_mark();
} else {
type_subject = "Int";
mark = new Int_mark();
auto *m = dynamic_cast<Int_mark*>(mark);
if (m)
m->print_mark();
}
}
Subject::~Subject() {
delete mark;
}
int main() {
Subject q;
}
Since I did not correctly understand the question in the first place, here a way how you can call the member function of base class Mark by object of derived class Int_Mark:
Int_mark *mark = new Int_mark();
mark->print_mark(); // calls member of the class Int_mark
mark->Mark::print_mark(); // calls member of the class Mark
Make sure that Mark::print_mark() is also defined and not just Int_mark::print_mark()

C++ getter method for private variable not working properly(Only have Java experience)

My header file
class Roster {
private:
Degree degree = NETWORK;
int addFlag = 0;
Student* classRosterArray[5] = { nullptr, nullptr, nullptr, nullptr, nullptr };
public:
void add(string studentID, string firstName, string lastName, string emailAddress,
int age, int daysToComplete1, int daysToComplete2, int daysToComplete3, Degree degreeProgram);
void remove(string studentId);
void printAll();
void printByDegreeProgram(int degreeProgram);
void printDaysInCourse(string studentId);
void printInvalidEmails();
Student* getClassRosterArray();
~Roster();
};
My getter method in CCP file
Student* Roster::getClassRosterArray() {
return *classRosterArray;
}
My main() in CCP
for (int k = 0; k < 5; k++) {
cout << "Student ID :: " << roster.getClassRosterArray()[k].getStudentId() << endl;
}
And it only takes out the first Student object, for the second index in array is empty....
Pleaese help! it is my first trying C++.!!
I will propose this change :
Student* getClassRosterArray( int ) ;
...
Student* Roster::getClassRosterArray(int id) {
return classRosterArray[id];
}
...
for (int k = 0; k < 5; k++) {
cout << "Student ID :: " << roster.getClassRosterArray(k)->getStudentId() << endl;
}
To retrieve the member without losing its size information, you might do
class Roster {
private:
Student* classRosterArray[5] = { nullptr, nullptr, nullptr, nullptr, nullptr };
// ...
public:
auto getClassRosterArray() -> Student* (&)[5] { return classRosterArray;}
// ...
};
Ugly syntax :/
std::array<Student*, 5> has more regular syntax:
class Roster {
private:
std::array<Student*, 5> classRosterArray{{ nullptr, nullptr, nullptr, nullptr, nullptr }};
// ...
public:
std::array<Student*, 5>& getClassRosterArray() { return classRosterArray;}
// ...
};
As Student seems not polymorphic, not using pointer seems even simpler with std::vector:
class Roster {
private:
std::vector<Student> classRosterArray;
// ...
public:
std::vector<Student>& getClassRosterArray() { return classRosterArray;}
// ...
};
You declare an array of Student*, in other words you declare a Student**.
The get method return the a Student* and not a Student** or better a Student*[].
This cause you problem. In other words you return only the first element of the array.

how do i initialize a public variable in a public class method

I have a public class in which I create an array, this array takes its size from the constructor and needs to be used in other functions (including int main). Therefore the variable must be public. my code looks something along these lines:
class myclass {
public:
int parameter1;
int parameter2;
myclass(int p, int p2) {
parameter1 = p;
parameter2 = p2;
}
void makeArray() {
int array[parameter1][parameter2]; //I want this array to be public as the next method needs access to it
}
void otherFunction() {
array[1][2] = 5; //just an example of what i need to do
}
}
Look up how to use pointers and dynamic memory..
To do what you want would be something like:
class myclass {
public:
int parameter1;
int parameter2;
int **a;
myclass(int p, int p2) {
parameter1 = p;
parameter2 = p2;
a = nullptr;
}
~myclass() {
// TODO: delete "a"
}
void makeArray() {
// TODO: delete "a" if it has already been allocated
a = new *int[parameter1];
for (int i = 0; i < parameter1; ++i) {
a[i] = new int[parameter2];
}
}
void otherFunction() {
// TODO: check that "a" has already been allocated
a[1][2] = 5; //just an example of what i need to do
}
}
You could also allocate the array in the constructor since you have the necessary information being passed in already.
This is more optimized way to do the same thing:
class myclass {
public:
int parameter1;
int parameter2;
int *array;
myclass(int p1, int p2) {
parameter1 = p1;
parameter2 = p2;
}
void makeArray() {
array = new int[parameter1*parameter2];
}
void otherFunction() {
// ary[i][j] is then rewritten as ary[i*sizeY+j]
array[1*parameter2+2] = 5;
}
};
int main()
{
int sizeX = 5;
int sizeY = 5;
myclass m1(sizeX,sizeY);
m1.makeArray();
m1.otherFunction();
cout << m1.array[1*sizeY+2] << endl;
return 0;
}

Map which stores derived objects

I have parent class:
class Data
{
public:
Data ( void ) { }
Virtual int Size ( void )
{
return 100;
}
protected:
map<string, Data*> m;
};
Classes that inherit from class Data:
class Struct : public Data
{
public:
Struct ( void ) { }
Struct & Add ( const string & name, Data x )
{
Data * tmp = new Data ( x );
m[name] = tmp;
return *this;
}
void Print ( void )
{
for ( const auto & tmp : m )
cout << tmp . first << " " << tmp . second -> Size () << endl;
}
};
class IntData : public Data
{
public:
IntData ( void ) { }
int Size ( void )
{
return 4;
}
};
class DoubleData : public Data
{
public:
DoubleData ( void ) { }
int Size ( void )
{
return 8;
}
};
main :
int main ( void )
{
Struct a;
a . Add ( "Integer",IntData () );
a . Print ();
return 0;
}
Current output : Integer 100
Expected output : Integer 4
I want to create a map which would hold various types of objects that are derived from Data class. But when i want to call method Size from stored object in map ( in this case IntData ) which should return 4 It always returns value from parent class Data. How could i fix that please?
There's your problem:
Data * tmp = new Data ( x );
The actual pointer you're putting into the map is an instance of the Data parent class. You're copy-constructing a new instance of the Data parent class from an argument that you're passing, by value, as a parameter.
You need to change this whole function to:
Struct & Add ( const string & name, Data *x)
{
m[name] = x;
return *this;
}
And the caller is now responsible for constructing a new instance of any subclass:
a . Add ( "Integer",new IntData);
Then, this will work as you intended.
Of course, this kind of an approach brings up various issues with memory leaks, etc..., so you're better off using std::shared_ptr. But that would be a different question...
I've rewritten your code for you.
#include <unordered_map>
#include <memory>
#include <iostream>
class Data {
public:
virtual ~Data(){}
virtual int Size() = 0;
};
class Struct : public Data {
std::unordered_map<std::string, std::unique_ptr<Data>> m;
public:
Struct& Add(const std::string& name, std::unique_ptr<Data> x) {
m[name] = std::move(x);
return *this;
}
void Print() {
for(const auto& tmp : m )
std::cout << tmp.first << " " << tmp.second->Size() << "\n";
}
int Size() override {
int sum = 0;
for (const auto& tmp : m)
sum += tmp.second->Size();
return sum;
}
};
class IntData : public Data {
public:
int Size( ) override { return 4; }
};
class DoubleData : public Data {
public:
DoubleData( ) { }
int Size( ) override { return 8; }
};
int main() {
Struct a;
a.Add("Integer", std::make_unique<IntData>() );
a.Print();
}
Your welcome.

I can't display variables of different types included in array

I have to do a program for college.
I have 3 classes already declared in the statement of the problem.
First class:
class piesa_a{
protected:
int id;
char *tip;
int pret;
};
Second class:
class piesa_b:public piesa_a
{
private:
float lungime;
bool bw;
};
Third class:
class piesa_c:public piesa_a
{
private:
int nr;
piesa_b *buf;
};
In main I need to create an array in which to store items such piesa_a, piesa_b, piesa_c. Then I have to sort items by price.
I have this code so far: http://pastebin.com/nx2FGSfe
The program is incomplete because it does not displays each item in the array.
I got stuck here. But if you display the array's elements when they are outside of it, it works.
SHORT: I have an error on line 143 and I want to solve it.
main.cpp:143:18: error: request for member ‘afisare’ in ‘*(v + ((unsigned int)(((unsigned int)i) * 4u)))’, which is of non-class type ‘piesa_a*’
The code is here:
#include <cstdlib>
#include<iostream>
#include<string.h>
using namespace std;
class piesa_a{
protected:
int id;
char *tip;
int pret;
public:
piesa_a()
{
id = 0;
tip = new char[1];
pret = 0;
}
piesa_a(int aidi, char *typ, int pretz)
{
id = aidi;
tip = new char[strlen(typ)+1];
strcpy(tip,typ);
pret = pretz;
}
piesa_a&operator =(piesa_a alta)
{
id = alta.id;
tip = new char[strlen(alta.tip)+1];
strcpy(tip,alta.tip);
pret = alta.pret;
return *this;
}
virtual void afisare()
{
cout<<"\n Piesa A: "<<id<<" "<<tip<<" "<<pret;
}
};
class piesa_b:public piesa_a
{
private:
float lungime;
bool bw;
public:
piesa_b():piesa_a(){lungime = 0;bw = 0;}
piesa_b(float lg,bool bl, int aid, char *tipi, int pretzz):piesa_a(aid,tipi,pretzz)
{
lungime = lg;
bw = bl;
}
piesa_b&operator =(piesa_b &c)
{
id = c.id;
tip = new char[strlen(c.tip)+1];
strcpy(tip,c.tip);
pret = c.pret;
lungime = c.lungime;
bw = c.bw;
return *this;
}
void afisare()
{
piesa_a::afisare();
cout<<"impreuna cu piesa B: "<<lungime<<" "<<bw<<"\n";
}
};
class piesa_c:public piesa_a
{
private:
int nr;
piesa_b *buf;
public:
piesa_c():piesa_a(){nr=0; buf = new piesa_b[nr];}
piesa_c(int n, piesa_b *bu,int aid, char *tipi, int pretzz):piesa_a(aid,tipi,pretzz)
{
nr = n;
buf = new piesa_b[nr];
for(int i=0;i<nr;i++)
buf[i]= bu[i];
}
piesa_c&operator =(piesa_c &alta)
{
id = alta.id;
tip = new char[strlen(alta.tip)+1];
strcpy(tip,alta.tip);
pret = alta.pret;
nr = alta.nr;
for(int i=0;i<alta.nr;i++)
buf[i] = alta.buf[i];
}
void afisare()
{
for(int i=0;i<nr;i++)
buf[i].afisare();
}
};
int main(int argc, char** argv) {
piesa_b *H;
H = new piesa_b[2];
piesa_a A(4,"TIPA",120);
piesa_b B(100,1,3,"TIPA",120);
H[0]=B;
H[1]=B;
piesa_c C(2, H,14,"TIPC",20);
piesa_a** v = new piesa_a*[3];
v[0] = &A;
v[1] = &B;
v[2] = &C;
for(int i=0;i<3;i++)
v[i].afisare();
return 0;
}
What's wrong?
In C++ (and current C), casts are almost always a sign that the programmer didn't know how to use the language as it is supposed to be used. If you need an array of 3 types of data, the cleanest solution is an array of objects of a class that is base to the 3. And if you want to display each item differently, you'll want to overload the << operator, so you just iterate over the array and go << on each item. Sorted by price means that the class includes a price field, and you use the sort from the standard template library, passing a comparison operation that just compares prices.