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;
}
}
}
}
Related
I have a problem with assigning variables to an array from different functions. I have two functions that produce different numbers. I then want to assign those numbers to a private array in the same class. When I do this the array returns large negative numbers.
// Array.h
class Array {
private:
int W = A;
int Q = B;
int sum[2] = {W, Q};
public:
int A;
int B;
int num1();
int num2();
int add();
};
// Array.cpp
#include<iostream>
using namespace std;
#include "Array.h"
int Array::num1()
{
int x = 3;
int y = 4;
A = x + y;
cout << A << endl;
return A;
}
int Array::num2()
{
int x = 2;
int y = 5;
B = x + y;
cout << B << endl;
return B;
}
int Array::add()
{
for(int i = 0; i < 2; i++)
{
cout << sum[i] << endl;
}
return 0;
}
// main.cpp
#include <iostream>
#include "Array.h"
int main() {
Array sumTotal;
sumTotal.num1();
sumTotal.num2();
sumTotal.add();
return 0;
}
Problem is here:
int W = A;
int Q = B;
int sum[2] = { W, Q };
You are just coping value from A and B to W and Q.
And later when you set A and B, those changes are not reflected to W or Q.
Thus leaving W and Q uninitialized.
Note: consider researching more about C++ topic in field of arrays, pointers and references.
This is modified code that works ok:
#include <iostream>
using namespace std;
class Array {
private:
int sum[2];
public:
int num1();
int num2();
int add();
};
int Array::num1()
{
int x = 3;
int y = 4;
sum[0] = x + y;
cout << sum[0] << endl;
return sum[0];
}
int Array::num2()
{
int x = 2;
int y = 5;
sum[1] = x + y;
cout << sum[1] << endl;
return sum[1];
}
int Array::add()
{
for (int i = 0; i < 2; i++)
{
cout << sum[i] << endl;
}
return 0;
}
int main(int argc, char** argv)
{
Array sumTotal;
sumTotal.num1();
sumTotal.num2();
sumTotal.add();
return 0;
}
The reason you are getting garbage values (large negative numbers, in your case) is that you are not initializing A or B to any meaningful values, and then you are not updating sum when you call num1, or num2.
You should initialize A and B to something meaningful in the class, or at least default initialize it.
Then you need to update sum in num1, like this:
int Array::num1()
{
int x = 3;
int y = 4;
A = x + y;
sum[0] = A; // <- add this
cout << A << endl;
return A;
}
and do a similar thing inside num2.
You also have 2 variables W, and Q inside your class which don't seem to serve any purpose. Apart from the issue with initializing them incorrectly with garbage values, you don't even need them; you could just use A, and B instead.
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.
This question might be simple but I never use raw pointers or arrays in C++ so...
I need to use a library function which looks like this:
void f(double a[3][3], double b[3], double c[3]);
a and b are used for input and the result is stored in c.
The computation of a is a bit complex but does never change so it makes sense to calculate it only once and save the result. In my program, I can link it to an object of type X.
class X{
public:
X(){
a = {{1,2,3},
{4,5,6},
{7,8,9}};
}
private:
double a[3][3];
}
How can I write a getter for X::a which can be used in function f?
This is how I would like to call function f:
#include "X.h"
int main(int argc, char *argv[]){
X o = X(); //create object
double a[3][3] = o.getA(); // I want to get a somehow
double b[3] = {1,2,3}; // create dummy b
double c[3] = {}; // create empty c
f(a,b,c); // call funktion to populate c
for(int i=0; i<3; ++i){
std::cout << c[i] << endl;
}
}
You know std::vector is the way to go for 2D arrays in C++, but if you can't bypass the obstacle you are facing, then it would be possible to pass the matrix as a parameter to the getter function, like this:
#include <iostream>
class X {
public:
void getA(double (&array)[3][3]) {
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
array[i][j] = a[i][j];
}
private:
double a[3][3] = {{1,2,3},
{4,5,6},
{7,8,9}};
};
int main(void) {
X o = X();
double a[3][3];
o.getA(a);
for(int i = 0; i < 3; ++i)
for(int j = 0; j < 3; ++j)
std::cout << a[i][j] << std::endl;
return 0;
}
This snippet should serve your purpose.
#include <iostream>
#include <string>
using namespace std;
class X {
public:
X() {
}
typedef double (*ptr_matrix)[3];
ptr_matrix getA(){
return a;
}
private:
double a[3][3] = {{ 1,2,3},
{4,5,6},
{7,8,9}};
};
void f(double a[3][3], double b[3], double c[3])
{
cout<<"Inside f function";
for(auto i = 0; i < 3;i++) {
cout<<endl;
for(auto j = 0 ; j < 3;j++)
cout<<a[i][j];
}
}
int main()
{
X o = X(); //create object
double (*a)[3] = NULL;
a = o.getA(); // I want to get a somehow
double b[3] = {0};
double c[3] = {0};
f(a,b,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.
Alright, I'm trying to implement a simple 2D matrix class right now. This is what it looks like so far:
template <typename Type>
class dyMatrix {
private:
Type *mat;
int width, height;
int length;
public:
dyMatrix (int _width, int _height)
: width(_width), height(_height), mat(0)
{
length = width * height;
mat = new Type[length];
};
// ---
int getWidth() {
return width;
};
int getHeight() {
return height;
};
int getLength() {
return length;
}
// ---
Type& operator() (int i, int j) {
return mat[j * width + i];
};
Type& operator() (int i) {
return mat[i];
};
// ---
~dyMatrix() {
delete[] mat;
};
};
To test it, and compare with static multi-dimensional arrays, I wrote the following snippet of code:
#include <iostream>
using namespace std;
/* matrix class goes here */
struct Coord {
int x, y;
Coord()
: x(0), y(0)
{};
Coord (int _x, int _y)
: x(_x), y(_y)
{};
void print() {
cout << x << ", " << y;
};
};
int main() {
dyMatrix<Coord> adabo(5, 7);
Coord inakos[5][7];
int i = 5, j = 0;
adabo(i, j) = *(new Coord(i, j));
inakos[i][j] = *(new Coord(i, j));
inakos[i][j].print();
adabo(i, j).print();
return 0;
}
"Adabo" and "Inakos" being arbitrarily chosen names. Upon execution, inakos prints its contents but the program crashes before adabo can do anything. Another interesting thing is that, if I give i and j values other than 5 and 0, like 5 and 1, respectively, it works fine.
I don't know what exact numbers work and which make the program go haywire, I only know that there's a irregularity here. What am I possibly doing wrong? I'm an amateur at C++, so I may or not have misused something in any of the structures.
If anyone also has the time, I'd very much like to know if there's any other error of notice in my matrix class. Anything that's not possibly related to the problem, but is a fallacy nevertheless.
I've also tested it with the following main(), but it still crashes after inakos prints its contents in [5][1]. Maybe it has to do not with dyMatrix, but a loosely-implemented Coord?
int main() {
dyMatrix<Coord> adabo(5, 7);
Coord inakos[5][7];
for (int i = 0; i < adabo.getHeight(); i++) {
for (int j = 0; j < adabo.getWidth(); j++) {
adabo(i, j) = *(new Coord(i, j));
inakos[i][j] = *(new Coord(i, j));
inakos[i][j].print();
cout << "; ";
}
cout << "\n\n";
}
cout << "\n\n\n";
Coord temp;
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 5; j++) {
temp = adabo(i, j);
temp.print();
cout << "; ";
}
cout << "\n\n";
}
return 0;
}
edit: It hasn't to do with Coord. Just tested with a dyMatrix of ints and static matrix of ints, and it crashes after [5][0] nevertheless.
In your first example, you declared inakos[5][7] so the indices range from 0 to 4 and 0 to 6. inakos[5][0] and inakos[5][1] could therefore crash.
In your second example, you again declare inakos[5][7] yet you let the first index loop from 0 to 6. Again inakos[i][j] can crash. One fix is to switch your indices (ie, change to i<adabo.getWidth() and j<adabo.getHeight()).