Why can't I change objects in a vector? - c++

I have a class TileGrid that holds an std::vector< std::vector<Tile> >. Accessing the Tile objects in the vector works, but I can't change their properties? For the sake of completion, here are all the relevant classes:
tilegrid.h
#include <vector>
#include "tile.h"
class TileGrid {
public:
TileGrid();
TileGrid(unsigned int rows, unsigned int cols);
virtual ~TileGrid();
unsigned int getRows() const { return rows_; };
unsigned int getCols() const { return cols_; };
Tile atIndex(unsigned int row, unsigned int col) const { return tiles_[row].at(col); };
private:
std::vector< std::vector<Tile> > tiles_;
unsigned int rows_;
unsigned int cols_;
};
tilegrid.cpp
#include "tilegrid.h"
TileGrid::TileGrid() : rows_(0), cols_(0) {
}
TileGrid::TileGrid(unsigned int rows, unsigned int cols) : rows_(rows), cols_(cols) {
tiles_.clear();
for (unsigned int y = 0; y < rows_; y++) {
std::vector<Tile> horizontalTiles;
for (unsigned int x = 0; x < cols_; x++) {
horizontalTiles.push_back(Tile());
}
tiles_.push_back(horizontalTiles);
}
}
TileGrid::~TileGrid() {
}
tile.h
class Tile {
public:
Tile();
virtual ~Tile();
bool isActive() const { return isActive_; };
void setActive(bool status) { isActive_ = status; };
private:
bool isActive_;
};
tile.cpp
#include "tile.h"
Tile::Tile() : isActive_(false) {
}
Tile::~Tile() {
}
main.cpp
#include "tilegrid.h"
#include <iostream>
int main() {
TileGrid tg(20, 20);
for (unsigned int i = 0; i < tg.getRows(); i++) {
for (unsigned int j = 0; j < tg.getCols(); j++) {
if (tg.atIndex(i, j).isActive()) {
std::cout << i << "," << j << " is active" << std::endl;
} else {
std::cout << i << "," << j << " is NOT active" << std::endl;
}
}
}
// This is all working. But when I for example use the setActive function, nothing changes:
tg.atIndex(1, 0).setActive(true);
// When I print it again, the values are still the ones that were set in the constructor
for (unsigned int i = 0; i < tg.getRows(); i++) {
for (unsigned int j = 0; j < tg.getCols(); j++) {
if (tg.atIndex(i, j).isActive()) {
std::cout << i << "," << j << " is active" << std::endl;
} else {
std::cout << i << "," << j << " is NOT active" << std::endl;
}
}
}
return 0;
}
I'm really sorry for all this code... I tried to keep it as short as possible, but I thought it'd be better to post it all!
So yeah, my problem is the setActive function. When I just create a Tile and call its setActive function, everything works, but when I call it through the TileGrid object, it won't.
I have tried to solve this on my own for hours and I can't think straight anymore. I'm really desperate here, could you please have a look and maybe help me?

In your method:
Tile atIndex(unsigned int row, unsigned int col) const
you should return a reference to Tile:
Tile& atIndex(unsigned int row, unsigned int col)
now you are returning copy, and that is why modifications does not work. Also it should not be const, otherwise you will get compiler error.

Related

'this' cannot be used in a constant expression error

The error is on line 76 int res[mSize]; the problem is on mSize. It seems like a simple fix but I can't figure it out. If someone can figure it out or point me in the right direction that would be greatly appreciated.
Also, the deconstructor ~MyContainer(), I am not sure if I am using it right or if there is a correct place to put it.
Here is my code:
#include <iostream>
using namespace std;
class MyContainer
{
private:
int* mHead; // head of the member array
int mSize; // size of the member array
public:
MyContainer();
MyContainer(int*, int);
//~MyContainer();
void Add(int);
void Delete(int);
int GetSize();
void DisplayAll();
int FindMissing();
~MyContainer() {}
};
MyContainer::MyContainer()
{
mHead = NULL;
mSize = 0;
}
MyContainer::MyContainer(int* a, int b)
{
mHead = a;
mSize = b;
}
void MyContainer::Add(int a)
{
*(mHead + mSize) = a;
mSize++;
}
void MyContainer::Delete(int a)
{
int index;
for (int i = 0; i < mSize; i++)
{
if (*(mHead + i) == a)
{
index = i;
break;
}
}
for (int i = index; i < mSize; i++)
{
*(mHead + i) = *(mHead + i + 1);
}
mSize--;
}
int MyContainer::GetSize()
{
return mSize;
}
void MyContainer::DisplayAll()
{
cout << "\n";
for (int i = 0; i < mSize; i++)
{
cout << *(mHead + i) << " ";
}
}
int MyContainer::FindMissing()
{
int res[mSize];
int temp;
int flag = 0;
for (int i = 1; i <= mSize; i++)
{
flag = 0;
for (int j = 0; j < mSize; j++)
{
if (*(mHead + j) == i)
{
flag = 1;
break;
}
}
if (flag == 0)
{
temp = i;
break;
}
}
return temp;
}
int main()
{
const int cSize = 5;
int lArray[cSize] = { 2, 3, 7, 6, 8 };
MyContainer lContainer(lArray, cSize);
lContainer.DisplayAll();
lContainer.Delete(7);
lContainer.DisplayAll();
cout << "Size now is: " << lContainer.GetSize() << endl; lContainer.Add(-1);
lContainer.Add(-10);
lContainer.Add(15);
lContainer.DisplayAll();
cout << "Size now is: " << lContainer.GetSize() << endl;
cout << "First missing positive is: " << lContainer.FindMissing() << endl;
system("PAUSE"); return 0;
}
int res[mSize];
The size of the array mSize must be known at compile time. You cannot use a variable here. An option may be to define a macro with an largish value that will not exceeded.
static const int kLargeSize =100;
int res[kLargeSize];
Edited in response to the comments - const and constexpr are a better option than a macro.
Or even better, you can use std::vector - https://en.cppreference.com/w/cpp/container/vector

declaring a function with arrays

First of all, im a c++ noob! Ok with that being said, i need to declare a function that initializes a grid. The function takes an array of int as the input and needs to return an array of int. I have:
array<int> InitializeGrid (array<int>)
{
const int NB_ROWS = 10;
const int NB_COLUMN = 10;
const int WATER = 0;
int grid[NB_ROWS][NB_COLONN];
for (int i = 0; i < NB_ROWS; i++)
{
for (int j = 0; j < NB_COLONN; j++)
{
grid[i][j] = WATER;
cout << grid[i][j] << " ";
}
cout << endl;
}
return ??
}
You don't need to return anything if you pass the array by reference:
#include <array>
#include <iostream>
static const int NB_ROWS = 10;
static const int NB_COLUMN = 10;
static const int WATER = 0;
void InitializeGrid (std::array<std::array<int, NB_COLUMN>, NB_ROWS> &grid)
{
for (auto &row : grid)
{
for (auto &col : row)
{
col = WATER;
std::cout << col << " ";
}
std::cout << '\n';
}
}
int main()
{
std::array<std::array<int, NB_COLUMN>, NB_ROWS> grid;
InitializeGrid(grid);
}
btw, if your WATER is 0 it is sufficive to write
std::array<std::array<int, NB_COLUMN>, NB_ROWS> grid{};
to initialize all elements to zero.

Overload assign after overloading [][]

What begun as a revision of some aspects of C++ has become a sketch of something that could be useful for me. The idea is to use a single array as a double array where indexing is transparent for the user.
class matrice {
private:
int* data;
int rows, cols;
public:
matrice(int _rows, int _cols) : rows(_rows), cols(_cols) { data = new int[_rows*_cols]; std::cout << "matrice ctr" << std::endl;}
// hardcoded 2x2 matrix
void set_value() { data[0] = 1; data[1] = 0; data[3] = 1; data[4] = 0;}
int get_rows() { return rows;}
int get_cols() { return cols;}
class proxy {
private:
int i;
int j;
const int rows; // it is constant so we don't mess up with the size of the matrix
int *proxy_data;
public:
proxy(int* _proxy_data, int _i, const int& _rows) : proxy_data(_proxy_data), i(_i), rows(_rows) { }
int operator[] (int _j) { j = _j; std::cout << "proxy:\n\tj = " << j << " " << proxy_data[j] << std::endl; return proxy_data[i*rows + j];}
};
proxy operator[] (int i) { std::cout << "matrice:\n\ti = " << i << std::endl; return proxy(data, i, rows); }
};
int main()
{
int rows = 2;
int cols = 2;
matrice matp(rows, cols);
matp.set_value();
matp[0][0] = 2;
for (int i = 0;i < rows;i++) {
for (int j = 0;j < cols;j++) {
std::cout << "matp[" << i << "][" << j << "] = " << matp[i][j] << std::endl;
}
}
}
So far I can access the data in the array however, I want to assign values to it i.e.:
matp[0][0] = 2;
How could I do it ?
int& operator[] (int j)const&&
Also delets int j member it is pointless.
int& operator[] (int j) const&& {
std::cout << "proxy:\n\tj = " << j << " " << proxy_data[j] << std::endl;
return proxy_data[i*rows + j];}
};
or better:
template<class X>
class proxy {
private:
X *proxy_data;
public:
proxy(X* _proxy_data, std::size_t i, std::size_t stride) : proxy_data(_proxy_data+i*stride) { }
X& operator[] (std::size_t j) const&& { return proxy_data[j]; }
};
use proxy<int> and proxy<const int> as the return value from [] and [] const.

Getter for dynamic array in c++ not working

I am using a dynamic array from boost in one of my classes and having trouble to write a proper getter function for it. Heres what I tried (I checked the size of the array within the class setter and with the getter from the main function):
#include <iostream>
#include "boost/multi_array.hpp"
using namespace std;
using namespace boost;
typedef multi_array<int, 3> array3i;
class Test {
private:
array3i test_array_;
void init(int x, int y, int z) {
array3i test_array_(extents[x][y][z]);
cout << "Size should be: " << test_array_.size() << endl;
for (int j=0; j<x; j++) {
for (int jj=0; jj<y; jj++) {
for (int jjj=0; jjj<z; jjj++) {
test_array_[j][jj][jjj] = j+jj+jjj;
}
}
}
};
public:
array3i test_array() {return test_array_;};
Test(int x, int y, int z) {
init(x, y, z);
};
};
int main(int argc, const char * argv[]) {
Test test(2,3,5);
cout << "Size from getter: " << test.test_array().size() << endl;
return 0;
}
The getter is working but you didn't initialize the member.
array3i test_array_(extents[x][y][z]);
initializes a local variable (that stops to exist after init() exits).
The problematic part was (likely) that you can't just assign a multi_array of different size/shape. So you need to use resize() (or initialize test_array_ shape in the constructor initializer list).
Live On Coliru
Fixed:
#include <iostream>
#include "boost/multi_array.hpp"
using namespace std;
using namespace boost;
typedef multi_array<int, 3> array3i;
class Test {
private:
array3i test_array_;
void init(int const x, int const y, int const z)
{
test_array_.resize(extents[x][y][z]);
cout << "Size should be: " << test_array_.size() << endl;
for (int j = 0; j < x; j++) {
for (int jj = 0; jj < y; jj++) {
for (int jjj = 0; jjj < z; jjj++) {
test_array_[j][jj][jjj] = j + jj + jjj;
}
}
}
};
public:
array3i test_array() { return test_array_; };
Test(int x, int y, int z) { init(x, y, z); };
};
int main()
{
Test test(2, 3, 5);
cout << "Size from getter: " << test.test_array().size() << endl;
}
The problem is probably that you have two test_array_ variables: One in the class as a class member, one local in the init function.
The local variables shadows, and overrides, the member variable.
In your init function you should be having:
void init(int const x, int const y, int const z)
{
//test_array_.resize(extents[x][y][z]);
test_array_ = array3i(extents[x][y][x]);
cout << "Size should be: " << test_array_.size() << endl;
for (int j = 0; j < x; j++) {
for (int jj = 0; jj < y; jj++) {
for (int jjj = 0; jjj < z; jjj++) {
test_array_[j][jj][jjj] = j + jj + jjj;
}
}
}
}

Vector of Vectors to create matrix

I am trying to take in an input for the dimensions of a 2D matrix. And then use user input to fill in this matrix. The way I tried doing this is via vectors (vectors of vectors). But I have encountered some errors whenever I try to read in data and append it to the matrix.
//cin>>CC; cin>>RR; already done
vector<vector<int> > matrix;
for(int i = 0; i<RR; i++)
{
for(int j = 0; j<CC; j++)
{
cout<<"Enter the number for Matrix 1";
cin>>matrix[i][j];
}
}
Whenever I try to do this, it gives me a subscript out of range error. Any advice?
You have to initialize the vector of vectors to the appropriate size before accessing any elements. You can do it like this:
// assumes using std::vector for brevity
vector<vector<int>> matrix(RR, vector<int>(CC));
This creates a vector of RR size CC vectors, filled with 0.
As it is, both dimensions of your vector are 0.
Instead, initialize the vector as this:
vector<vector<int> > matrix(RR);
for ( int i = 0 ; i < RR ; i++ )
matrix[i].resize(CC);
This will give you a matrix of dimensions RR * CC with all elements set to 0.
I'm not familiar with c++, but a quick look at the documentation suggests that this should work:
//cin>>CC; cin>>RR; already done
vector<vector<int> > matrix;
for(int i = 0; i<RR; i++)
{
vector<int> myvector;
for(int j = 0; j<CC; j++)
{
int tempVal = 0;
cout<<"Enter the number for Matrix 1";
cin>>tempVal;
myvector.push_back(tempVal);
}
matrix.push_back(myvector);
}
Assume we have the following class:
#include <vector>
class Matrix {
private:
std::vector<std::vector<int>> data;
};
First of all I would like suggest you to implement a default constructor:
#include <vector>
class Matrix {
public:
Matrix(): data({}) {}
private:
std::vector<std::vector<int>> data;
};
At this time we can create Matrix instance as follows:
Matrix one;
The next strategic step is to implement a Reset method, which takes two integer parameters that specify the new number of rows and columns of the matrix, respectively:
#include <vector>
class Matrix {
public:
Matrix(): data({}) {}
Matrix(const int &rows, const int &cols) {
Reset(rows, cols);
}
void Reset(const int &rows, const int &cols) {
if (rows == 0 || cols == 0) {
data.assign(0, std::vector<int>(0));
} else {
data.assign(rows, std::vector<int>(cols));
}
}
private:
std::vector<std::vector<int>> data;
};
At this time the Reset method changes the dimensions of the 2D-matrix to the given ones and resets all its elements. Let me show you a bit later why we may need this.
Well, we can create and initialize our matrix:
Matrix two(3, 5);
Lets add info methods for our matrix:
#include <vector>
class Matrix {
public:
Matrix(): data({}) {}
Matrix(const int &rows, const int &cols) {
Reset(rows, cols);
}
void Reset(const int &rows, const int &cols) {
data.resize(rows);
for (int i = 0; i < rows; ++i) {
data.at(i).resize(cols);
}
}
int GetNumRows() const {
return data.size();
}
int GetNumColumns() const {
if (GetNumRows() > 0) {
return data[0].size();
}
return 0;
}
private:
std::vector<std::vector<int>> data;
};
At this time we can get some trivial matrix debug info:
#include <iostream>
void MatrixInfo(const Matrix& m) {
std::cout << "{ \"rows\": " << m.GetNumRows()
<< ", \"cols\": " << m.GetNumColumns() << " }" << std::endl;
}
int main() {
Matrix three(3, 4);
MatrixInfo(three);
}
The second class method we need at this time is At. A sort of getter for our private data:
#include <vector>
class Matrix {
public:
Matrix(): data({}) {}
Matrix(const int &rows, const int &cols) {
Reset(rows, cols);
}
void Reset(const int &rows, const int &cols) {
data.resize(rows);
for (int i = 0; i < rows; ++i) {
data.at(i).resize(cols);
}
}
int At(const int &row, const int &col) const {
return data.at(row).at(col);
}
int& At(const int &row, const int &col) {
return data.at(row).at(col);
}
int GetNumRows() const {
return data.size();
}
int GetNumColumns() const {
if (GetNumRows() > 0) {
return data[0].size();
}
return 0;
}
private:
std::vector<std::vector<int>> data;
};
The constant At method takes the row number and column number and returns the value in the corresponding matrix cell:
#include <iostream>
int main() {
Matrix three(3, 4);
std::cout << three.At(1, 2); // 0 at this time
}
The second, non-constant At method with the same parameters returns a reference to the value in the corresponding matrix cell:
#include <iostream>
int main() {
Matrix three(3, 4);
three.At(1, 2) = 8;
std::cout << three.At(1, 2); // 8
}
Finally lets implement >> operator:
#include <iostream>
std::istream& operator>>(std::istream& stream, Matrix &matrix) {
int row = 0, col = 0;
stream >> row >> col;
matrix.Reset(row, col);
for (int r = 0; r < row; ++r) {
for (int c = 0; c < col; ++c) {
stream >> matrix.At(r, c);
}
}
return stream;
}
And test it:
#include <iostream>
int main() {
Matrix four; // An empty matrix
MatrixInfo(four);
// Example output:
//
// { "rows": 0, "cols": 0 }
std::cin >> four;
// Example input
//
// 2 3
// 4 -1 10
// 8 7 13
MatrixInfo(four);
// Example output:
//
// { "rows": 2, "cols": 3 }
}
Feel free to add out of range check. I hope this example helps you :)
try this. m = row, n = col
vector<vector<int>> matrix(m, vector<int>(n));
for(i = 0;i < m; i++)
{
for(j = 0; j < n; j++)
{
cin >> matrix[i][j];
}
cout << endl;
}
cout << "::matrix::" << endl;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
Vector needs to be initialized before using it as cin>>v[i][j]. Even if it was 1D vector, it still needs an initialization, see this link
After initialization there will be no errors, see this link
What you have initialized is a vector of vectors, so you definitely have to include a vector to be inserted("Pushed" in the terminology of vectors) in the original vector you have named matrix in your example.
One more thing, you cannot directly insert values in the vector using the operator "cin". Use a variable which takes input and then insert the same in the vector.
Please try this out :
int num;
for(int i=0; i<RR; i++){
vector<int>inter_mat; //Intermediate matrix to help insert(push) contents of whole row at a time
for(int j=0; j<CC; j++){
cin>>num; //Extra variable in helping push our number to vector
vin.push_back(num); //Inserting numbers in a row, one by one
}
v.push_back(vin); //Inserting the whole row at once to original 2D matrix
}
I did this class for that purpose. it produces a variable size matrix ( expandable) when more items are added
'''
#pragma once
#include<vector>
#include<iostream>
#include<iomanip>
using namespace std;
template <class T>class Matrix
{
public:
Matrix() = default;
bool AddItem(unsigned r, unsigned c, T value)
{
if (r >= Rows_count)
{
Rows.resize(r + 1);
Rows_count = r + 1;
}
else
{
Rows.resize(Rows_count);
}
if (c >= Columns_Count )
{
for (std::vector<T>& row : Rows)
{
row.resize(c + 1);
}
Columns_Count = c + 1;
}
else
{
for (std::vector<T>& row : Rows)
{
row.resize(Columns_Count);
}
}
if (r < Rows.size())
if (c < static_cast<std::vector<T>>(Rows.at(r)).size())
{
(Rows.at(r)).at(c) = value;
}
else
{
cout << Rows.at(r).size() << " greater than " << c << endl;
}
else
cout << "ERROR" << endl;
return true;
}
void Show()
{
std::cout << "*****************"<<std::endl;
for (std::vector<T> r : Rows)
{
for (auto& c : r)
std::cout << " " <<setw(5)<< c;
std::cout << std::endl;
}
std::cout << "*****************" << std::endl;
}
void Show(size_t n)
{
std::cout << "*****************" << std::endl;
for (std::vector<T> r : Rows)
{
for (auto& c : r)
std::cout << " " << setw(n) << c;
std::cout << std::endl;
}
std::cout << "*****************" << std::endl;
}
// ~Matrix();
public:
std::vector<std::vector<T>> Rows;
unsigned Rows_count;
unsigned Columns_Count;
};
'''