How to access class variable inside a vector iterator? - c++

I'd like to access a public variable of a class instance, where the instances are kept in a vector of the class type. I have to run through all elements of vector using an iterator, but it confuses me as to how I get the variables with the iterator present. I'm using C++98.
source.cpp:
#include <iostream>
#include <vector>
#include "Rectangle.h"
using namespace std;
int main() {
int len = 2, hen = 5;
int len2 = 4, hen2 = 10;
Rectangle rect1(len, hen);
Rectangle rect2(len2, hen2);
vector<Rectangle> Rects;
Rects.push_back(rect1);
Rects.push_back(rect2);
for (std::vector<Rectangle>::iterator it = Rects.begin(); it != Rects.end(); ++it) {
//how to access length and height here?
}
system("pause");
return 0;
}
Rectangle.h:
#pragma once
class Rectangle
{
private:
public:
int length;
int height;
Rectangle(int& length, int& height);
~Rectangle();
};
Rectangle.cpp:
#include "Rectangle.h"
Rectangle::Rectangle(int& length, int& height)
: length(length), height(height)
{ }
Rectangle::~Rectangle() {}

Add the rectangle to vector first, dereference iterator and access the elements.
int main() {
int len = 2, hen = 5;
int len2 = 4, hen2 = 10;
Rectangle rect1(len, hen);
Rectangle rect2(len2, hen2);
vector<Rectangle> Rects;
Rects.push_back(rect1);
Rects.push_back(rect2);
for (std::vector<Rectangle>::iterator it = Rects.begin(); it != Rects.end(); ++it) {
std::cout << "length " <<(*it).length<<std::endl;
std::cout << "height " <<(*it).height<<std::endl;
}
system("pause");
return 0;
}

Related

How can I find a minimum value from a vector of entities?

class enemy{
....
}
std::vector<std::unique_ptr<enemy> > enemies1;
for (unsigned i = 0; i < 3; ++i)
enemies1.emplace_back(...);
for (int i = 0; i < enemies1.size(); i++) {
std::cout << i <<"x: " << enemies1[i]->rect.getPosition().x << std::endl;
}
output:
100
200
400
How could I get the minimum coordinate value from multiple enemies in the vector? I want to detect the nearest enemy from the player, eg the player's coordinate is 50 and enemies are at 100, 200, 400, as you see in the above example. I want to detect the nearest enemy in the vector.
You can use min_element from
#include <algorithm>
#include <iostream>
#include <vector>
struct enemy_t
{
explicit enemy_t(const double d) :
distance{ d }
{
}
double distance;
};
int main()
{
// create a vector of enemies using the constructor with one double
std::vector<enemy_t> enemies{ 100.0,400.0,200.0,10.0 };
// the last argument to min_element is a lambda function
// it helps you define by what condition you want to find your element.
auto enemy = std::min_element(enemies.begin(), enemies.end(), [](const enemy_t& lhs, const enemy_t& rhs)
{
return lhs.distance < rhs.distance;
});
std::cout << "The minimum distance found = " << enemy->distance << "\n";
return 0;
}
For finding out the minimum you can use :
auto result = std::min_element(enemies.begin(), enemies.end(), [](auto a, auto b){return a->rect.getPosition()< b->rect.getPosition();});
std::cout<<"minimum is: "<<(**result).rect.getPosition()<<std::endl;
The above example will print out the position of the closest(minimum) enemy as you want. You just need to add the above two statements into your program.
To confirm that this works(compile and gives the expected result), below i have given an example whose output can be seen here.
#include <vector>
#include <iostream>
#include <algorithm>
struct Rectangle
{
int getPosition() const
{
return position;
}
Rectangle(int p):position(p)
{
}
int position = 0;
};
struct enemy
{
Rectangle rect;
enemy(int p): rect{p}
{
}
};
int main()
{
std::vector<enemy*> enemies;
enemy e1(600),e2(200),e3(400),e4(300), e5(100);
enemies.push_back(&e1);
enemies.push_back(&e2);
enemies.push_back(&e3);
enemies.push_back(&e4);
enemies.push_back(&e5);
auto result = std::min_element(enemies.begin(), enemies.end(), [](auto a, auto b){return a->rect.getPosition()< b->rect.getPosition();});
std::cout<<"minimum is: "<<(**result).rect.getPosition()<<std::endl;
return 0;
}

Error C2280 : Class::Class(void) : Attempting to reference a deleted function

So, I am working on a project, and I have two files in this project:
main.cpp, matrix.h
The problem is that My code seemed to work perfectly a few hours ago, and now it doesn't
main.cpp:
#include <iostream>
#include "matrix.h"
#include <vector>
int main() {
Matrix f;
f.create(10, 1, {3, 4, 5, 6, 7, 8, 9});
}
matrix.h:
#pragma once
#include <iostream>
#include <string>
#include <Windows.h>
#include <vector>
class Matrix {
public:
const size_t N;
bool ifMatrixCreated;
const char* NOTENOUGH = "The size of the array should match to the width*height elements";
std::vector<int> arr;
int w, h;
void create(int width, int height, const std::vector<int> a) {
w = width;
h = height;
if (a.size() != width * height) {
ifMatrixCreated = false;
std::cout << "bello";
}
else {
ifMatrixCreated = true;
arr = a;
std::cout << "hello";
}
}
};
And when I compile, it generates this error (Using VS2019):
Error C2280 | 'Matrix::Matrix(void)': attempting to reference a deleted function Matrix | Line 5
It keeps saying that "The default constructor of Matrix cannot be referenced - It is a deleted function"
Can you help solve this error?
Thanks in advance.
Here is the correct working example. The error happens because every const data member must be initialized. And
The implicitly-declared or defaulted default constructor for class T is undefined (until C++11)defined as deleted (since C++11) if any of the following is true:
T has a const member without user-defined default constructor or a brace-or-equal initializer (since C++11).
#pragma once
#include <iostream>
#include <string>
//#include <Windows.h>
#include <vector>
class Matrix {
public:
//const size_t N;//this const data member must be initialised
const size_t N = 6;
bool ifMatrixCreated;
const char* NOTENOUGH = "The size of the array should match to the width*height elements";
std::vector<int> arr;
int w, h;
void create(int width, int height, const std::vector<int> a) {
w = width;
h = height;
if (a.size() != width * height) {
ifMatrixCreated = false;
std::cout << "bello";
}
else {
ifMatrixCreated = true;
arr = a;
std::cout << "hello";
}
}
};

Return pointer to array virtual template function

I would like to return an array to a pointer, in a virtual function that is a member of a derived class of a template class. In details, my classes definition is:
Sampler.h
#ifndef SAMPLER_H
#define SAMPLER_H
template <class T>
class Sampler
{
public:
virtual T getnumber()=0;
virtual T* simulation(int n)=0;
};
class UniformSampler:public Sampler<double>
{
public:
virtual double getnumber();
virtual double* simulation(int n);
UniformSampler(double a=0.0, double b=1.0);
private:
double low_bound;
double up_bound;
};
#endif
The class Sampler is a template class in order to be able to derive an other sampler with vectors later. The implementation is:
Sampler.cpp
#include "Sampler.h"
#include<iostream>
#include<cstdlib>
#include<cmath>
using namespace std;
//Uniform
UniformSampler::UniformSampler(double a, double b)
{
low_bound=a;
up_bound=b;
}
double UniformSampler::getnumber()
{
int myrand=rand();
while((myrand==0)||(myrand==RAND_MAX)){myrand = rand(); } //We want a number in (0, RAND_MAX).
double myuni = myrand/static_cast<double>(RAND_MAX); //Create a number in (0,1).
return low_bound + myuni*(up_bound-low_bound);
}
double* UniformSampler::simulation(int n){
double simulations[n];
for(int i=0; i<n; i++){
simulations[i] = this->getnumber();
}
return simulations;
}
My problem is that, when I try to call this program in the main(), it looks like the assignment of the pointer doesn't work. Here is my main.cpp:
#include <iostream>
#include <math.h>
#include <cstdlib>
#include <time.h>
using namespace std;
#include "Sampler.h"
int main(){
srand(time(0));
int n=10;
double *unif = new double[n];
UniformSampler uni;
unif = uni.simulation(n);
for ( int i = 0; i < n; i++ ) {
cout << "*(p + " << i << ") : ";
cout << *(unif + i) << endl;
}
delete[] unif;
return 0;
}
When I run it, it doesn't print any of the elements that unif points to. I don't understand what is wrong there.
UniformSampler::simulation is twice wrong:
double simulations[n]; uses VLA extension, so not C++ standard compliant.
you return pointer on local variable, so dangling pointer.
Solution: use std::vector instead.
#include <vector>
template <class T>
class Sampler
{
public:
virtual ~Sampler() = default;
virtual T getnumber() = 0;
virtual std::vector<T> simulation(int n) = 0;
};
class UniformSampler:public Sampler<double>
{
public:
explicit UniformSampler(double a=0.0, double b=1.0);
double getnumber() overrid;
std::vector<double> simulation(int n) override
{
std::vector<double> res(n);
for (auto& val : res){
res = getnumber();
}
return res;
}
private:
double low_bound;
double up_bound;
};
int main(){
srand(time(0));
constexpr int n = 10;
UniformSampler uni;
auto unif = uni.simulation(n);
for (int i = 0; i < n; i++ ) {
std::cout << "p[" << i << "]: " << unif[i] << endl;
}
}

Can't copy newly created objects in the Class constructor to its vector member in C++

In the class constructor, I am initializing other objects and pushing these objects to my class vector member. From what I understand, the vector create a copy of the object and stores it so that it doesn't go out of scope. However, when verifying the objects in another class function, they are not initialized anymore. Here's a example code to explain the behaviour:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
class Square {
private:
int size_ = 0;
int colour_ = 0;
public:
Square(){
size_ = 0;
colour_ = 0;
}
void init(int size, int colour) {
size_ = size;
colour_ = colour;
}
int get_size() { return size_; }
};
class SetSquares {
private:
std::vector<Square> squares_;
int number_;
public:
SetSquares(): number_(0) {}
void init(int num) {
number_ = num;
squares_.clear();
squares_.resize(num);
for (int i=0; i < num; i++) {
Square square;
square.init(i, i);
squares_.push_back(square);
}
}
void sample(int i) {
if (i >= number_) { return; }
std::cout << "Square size is: " << squares_[i].get_size() << std::endl;
}
};
int main()
{
SetSquares set_of_squares;
set_of_squares.init(7);
set_of_squares.sample(4);
return 0;
}
resize(n) will create n default constructed elements in a vector and push_back will append new elements after those n elements. Use reserve and push_back or resize and index operator as suggested in comment.

For-Loop Segmentation -- Excessive run ( i < SIZE, but i = SIZE)

This is a function in a program replicating Sierpinski's gasket. This function is supposed to attach the points in the triangle for the fractal.
After much deliberation I've figured out where the issue lies:
void add_pts(int &x, int &y)
{
Vector_ref<Rectangle> pt;
for (int i = 0; i < POINTS; ++i)
{
pt_test; //generates changing x, y vals within the limits of the triangle
cout << "pass" << i <<endl;
pt.push_back(new Rectangle(Point(x,y),5,5));
pt[i].set_fill_color(Color::yellow);
win.attach(pt[i]);
}
}
The output is "pass1...pass[POINTS-1]", but for whatever reason it runs when i = POINTS and runs into the segmentation error. I have no clue as to why. Can anyone assist, please?
Here is my code. The pt_test and coord are a bit sloppy but seeing as it can't run properly it's very hard to ascertain what I can streamline.
#include <stdlib.h>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <time.h>
#include "Simple_window.h"
#include "Graph.h"
#include "Point.h"
#include "GUI.h"
#include "Window.h"
using namespace std;
using namespace Graph_lib;
// globals
const int POINTS = 5000;
unsigned int seed = (unsigned int)time(0);
Simple_window win(Point(100,100),1100,700,"Homework 9");
// function declarations
double random(unsigned int &seed);
bool coords(int &x, int &y);
void pt_test(int x, int y);
void add_pts(int &x, int &y);
int main()
{
int x, y;
// title
Text title(Point(400,50), "The Sierpinski Gasket");
title.set_font(Graph_lib::Font::helvetica_bold);
title.set_font_size(25);
title.set_color(Color::cyan);
win.attach(title);
// triangle
Closed_polyline tri;
tri.add(Point(250,75)); // A
tri.add(Point(850,75)); // B
tri.add(Point(550,675)); // C
tri.set_fill_color(Color::white);
tri.set_color(Color::dark_red);
tri.set_style(Line_style(Line_style::solid,3));
win.attach(tri);
// vertices
Text vert_a(Point(225,70), "A (250, 75)");
vert_a.set_font(Graph_lib::Font::helvetica_bold);
vert_a.set_font_size(15);
vert_a.set_color(Color::cyan);
Text vert_b(Point(855,70), "B (850, 75)");
vert_b.set_font(Graph_lib::Font::helvetica_bold);
vert_b.set_font_size(15);
vert_b.set_color(Color::cyan);
Text vert_c(Point(575,670), "C (550, 675)");
vert_c.set_font(Graph_lib::Font::helvetica_bold);
vert_c.set_font_size(15);
vert_c.set_color(Color::cyan);
win.attach(vert_a);
win.attach(vert_b);
win.attach(vert_c);
// point selection
add_pts(x, y);
// window title and display
win.wait_for_button();
}
double random(unsigned int &seed)
{
const int MODULUS = 15749;
const int MULTIPLIER = 69069;
const int INCREMENT = 1;
seed = ((MULTIPLIER*seed)+INCREMENT)%MODULUS;
return double(seed)/double(MODULUS);
}
bool coords(int &x, int &y) // generates the points
{
x = int(251 + 600*random(seed));
y = int(76 + 600*random(seed));
if( y > (2*x-425) && x<= 550 || x>=550 && y < (-2*x + 1775))
return true;
}
void pt_test(int x, int y) // tests the points until they are within the range
{
coords;
while(coords == 0)
coords;
}
void add_pts(int &x, int &y) // attaches the points as shapes
{
Vector_ref<Rectangle> pt;
for (int i = 0; i < POINTS; ++i)
{
pt_test;
cout << "i == " << i << " points == " << POINTS << endl;
pt.push_back(new Rectangle(Point(x,y),5,5));
pt[i].set_fill_color(Color::yellow);
win.attach(pt[i]);
}
}
I've also noticed that the function add_pts doesn't work when the body is in the loop, but if you put the body in int_main(), it runs indefinitely but doesn't reach the segmentation fault as quickly, if at all.