'Invalid use of void expression' passing arguments of functions - c++

This is a piece of my code that solves systems of differential equations.
Vector dydx(Neq);
void DiffEq(Vector x, Vector &dydx)
{
dydx(0) = x(1);
dydx(1) = -x(0);
}
double MidPoint(int n, Vector x)
{
double h=H/n;
Matrix z(n+1,n+1);
z.fillRow(0,x);
DiffEq(x, dydx);
z.fillRow(1,AddVec(x, dydx*h)); //Error: Invalid use of void expression
for (int j=1; j<n; j++)
{
DiffEq(z.getRow(j), dydx);
z.fillRow(j+1, AddVec(z.getRow(j-1), dydx*h*2)); //error: void value not ignored as it ought to be
}
DiffEq(z.getRow(n), dydx);
return 0.5*AddVec(z.getRow(n), z.getRow(n-1), dydx*h); //Error: Invalid use of void expression
}
The classes Vector and Matrix are custom. This is Vector
class Vector
{
public:
Vector(size_t size): vSize(size), vData(size){}
int getSize(){return vSize;}
double& operator()(size_t i){return vData[i];}
double operator()(size_t i) const {return vData[i];}
void operator+(double d) // These only overload the Vector + int operator
{
for (int i=0; i<vSize; i++) {vData[i]=vData[i]+d;}
}
void operator*(double d)
{
for (int i=0; i<vSize; i++) {vData[i]=vData[i]*d;}
}
size_t vSize;
vector<double> vData;
};
With a function
Vector AddVec(Vector v1, Vector v2)
{
Vector totVec(v1.getSize());
for (int i=0; i<v1.getSize(); i++) {totVec(i) = v1(i) + v2(i);}
return totVec;
}
And the same function for 3 vectors.
Now I realise that the error means that I'm passing void to some function, but I cannot figure out where things go wrong. When I try writing a test program, everything seems to work fine.

Your operators return void
void operator+(double d) // These only overload the Vector + int
{
for (int i=0; i<vSize; i++) {vData[i]=vData[i]+d;}
}
void operator*(double d)
{
for (int i=0; i<vSize; i++) {vData[i]=vData[i]*d;}
}
So when you call AddVec(x, dydx*h) it calls AddVec(x, void)

You've overloaded operators that don't behave like the conventional ones, don't do that!!! Doing that gives C++ a bad name.
void operator*(double d)
{
for (int i=0; i<vSize; i++) {vData[i]=vData[i]*d;}
}
This doesn't return anything, so you can't use it like this:
z.fillRow(1,AddVec(x, dydx*h));
^^^^^^
operator* should return a new object, not modify its left operand. If you want to modify the left operand, use operator*= but you should still return something.
Vector& operator*=(double d)
{
for (int i=0; i<vSize; i++)
vData[i] *= d;
return *this;
}
Vector operator*(double d)
{
Vector v(*this);
v *= d;
return v;
}
The operator* would be better as a non-member, and should take its argument by reference:
Vector operator*(const Vector& v, double d)
{
Vector v2(v);
v2 *= d;
return v2;
}

Related

Error while overloading operator + in myVector class

I know this question has appeared lots of times here and on the internet, but even by searching on this site I can't overload the + operator in myvec class. The strange thing is that I can overload the = operator, but as I write the declaration and definition of the operator +, I get an error.
More specifically, I declare the operator as
myvec& myvec::operator+(const myvec& v, const myvec& w)
and the definition is
myvec& myvec::operator +(const myvec& v, const myvec& w)
{
int d = v.size();
myvec x(d);
for (int i = 0; i < d; i++) {
x(i) = v(i)+w(i);
}
return x;
}
In the following my little class:
#include <iostream>
#include <cmath>
using namespace std;
class myvec {
private:
int dimension;
double* data;
public:
myvec(int dim);
myvec(const myvec& v);
~myvec();
int size() const;
void Print();
double& operator ()(int i);
myvec& operator =(const myvec& v);
myvec& operator +(const myvec& v, const myvec& w);
};
myvec::myvec(int dim)
{
dimension = dim;
data = new double[dim];
for (int i = 0; i < dimension; i++) {
data[i] = 0.0;
}
}
myvec::myvec(const myvec& v)
{
int dimensione = v.size();
data = new double[dimensione];
for (int i = 0; i < dimensione; i++) {
data[i] = v.data[i];
}
}
myvec::~myvec()
{
dimension = 0;
delete[] data;
data = NULL;
}
int myvec::size() const
{
return dimension;
}
double& myvec::operator ()(int i)
{
return data[i];
}
myvec& myvec::operator =(const myvec& v)
{
int dim = v.size();
for (int i = 0; i < dim; ++i) {
data[i] = v.data[i];
}
return *this;
}
myvec& myvec::operator +(const myvec& v, const myvec& w)
{
int d = v.size();
myvec x(d);
for (int i = 0; i < d; i++) {
x(i) = v(i)+w(i);
}
return x;
}
void myvec::Print()
{
for (int i = 0; i < dimension; i++) {
cout << data[i]<<endl;
}
}
The compiler gives me the error:
testmyvec.cpp.cc:77:59: error: ‘myvec& myvec::operator+(const myvec&, const myvec&)’ must take either zero or one argument
It's clearly referring to the definition of the + operator. How can I fix it in order to overload the operator?
Complier already gave you the answer: "must take either zero or one argument".
Declaration should be myvec operator+(const myvec&); and in definition you use this like so:
myvec myvec::operator +(const myvec& w)
{
int d = v.size();
myvec x(d);
for (int i = 0; i < d; i++) {
x(i) = (*this)(i) + w(i);
}
return x;
}
And don't return reference to local objects.
Move the overloaded operator out of the myvec class. Also don't return references to local variables, change the return type to myvec.
class myvec{
...
};
myvec operator+(const myvec& v, const myvec& w);
myvec operator+(const myvec& v, const myvec& w) {
int d = v.size();
myvec x(d);
for(int i=0;i<d;i++) {
x(i)=v(i)+w(i);
}
return x;
}
operator+ takes two arguments, if you declare it inside a class and you give it two regular arguments then you've ended up with an operator+ that is taking three arguments, which isn't allowed.
For comprehensive advice look here

C++, overload * for matrix multiplication

I'm having a great deal of trouble trying to overload the multiplication operator * for matrix multiplication. I've defined a matrix class
#ifndef MMATRIX_H
#define MMATRIX_H
#include <vector>
#include <cmath>
// Class that represents a mathematical matrix
class MMatrix
{
public:
// constructors
MMatrix() : nRows(0), nCols(0) {}
MMatrix(int n, int m, double x = 0) : nRows(n), nCols(m), A(n * m, x)
{}
// set all matrix entries equal to a double
MMatrix &operator=(double x)
{
for (int i = 0; i < nRows * nCols; i++)
A[i] = x;
return *this;
}
// access element, indexed by (row, column) [rvalue]
double operator()(int i, int j) const
{
return A[j + i * nCols];
}
// access element, indexed by (row, column) [lvalue]
double &operator()(int i, int j)
{
return A[j + i * nCols];
}
// size of matrix
int Rows() const { return nRows; }
int Cols() const { return nCols; }
// operator overload for matrix * vector. Definition (prototype) of member class
MVector operator*(const MMatrix& A);
private:
unsigned int nRows, nCols;
std::vector<double> A;
};
#endif
And here is my attempted operator overload
inline MMatrix operator*(const MMatrix& A, const MMatrix& B)
{
MMatrix m(A), c(m.Rows(),m.Cols(),0.0);
for (int i=0; i<m.Rows(); i++)
{
for (int j=0; j<m.Cols(); j++)
{
for (int k=0; k<m.Cols(); k++)
{
c(i,j)+=m(i,k)*B(k,j);
}
}
}
return c;
}
I'm sure that there is nothing wrong with the actual multiplication of elements.
The error that I get is from my main .cpp file where I have tried to multiply two matrices together C=A*B; and I get this error,
error: no match for 'operator=' (operand types are 'MMatrix' and 'MVector')
There are 2 ways to overload operator*:
MMatrix MMatrix::operator*(MMatrix); //or const& or whatever you like
MMatrix operator*(MMatrix, MMatrix);
These are both valid, but different with slightly different semantics.
For your definition to match your declaration change the definition to:
MMatrix MMatrix::operator*(const MMatrix & A)
{
//The two matrices to multiple are (*this) and A
MMatrix c(Rows(),A.Cols(),0.0);
for (int i=0; i < Rows(); i++)
{
for (int j=0; j < A.Cols(); j++)
{
for (int k=0; k < Cols(); k++)
{
c(i,j) += (*this)(i,k)*A(k,j);
}
}
}
return c;
}
As for the error you're seeing, it seems in your class you declared the operator to take a matrix and return a vector. You probably meant to return a matrix instead. The error is telling you that you can't assign a MVector to a MMatrix.
I believe, you need to define copy constructor and copy assignment:
MMatrix(const MMatrix& other);
MMatrix& operator=(const MMatrix& other);
Move constructor and assignment wouldn't heart either:
MMatrix(MMatrix&& other);
MMatrix& operator=(MMatrix&& other);
Same goes to your MVector.

C++: Access via overloaded array subscript operator in 2D array

For the following function that represents a matrix
class m // matrix
{
private:
double **matrix;
int nrows, ncols;
class p
{
private:
double *arr;
public:
p (double *a)
: arr (a)
{
}
double &operator[] (int c)
{
return arr[c];
}
};
public:
m (int nrows, int ncols)
{
this->matrix = new double *[nrows];
for (int i = 0; i < nrows; ++i)
{
this->matrix[i] = new double [ncols];
}
this->nrows = nrows;
this->ncols = ncols;
}
~m()
{
for (int i = 0; i < this->nrows; ++i)
{
delete [] this->matrix[i];
}
delete this->matrix;
}
void assign (int r, int c, double v)
{
this->matrix[r][c] = v;
}
p operator[] (int r)
{
return p (this->matrix[r]);
}
};
operator works for element access but does not work with element change. How can I add the functionality of assign() function to the operator?
Redefine your first accessor as
const p& operator[] (int r) const
and the one for setting as
p& operator[] (int r)
Then ensure that a value copy is not taken in either case. Returning a reference allows you to change the element value via the calling function.
Your p class is private and you won't be able to call operator [] on it.
Make p accessible, and in both operator [] implementations you should return by reference, not by value. This will allow you to modify original data, not the copy's.

How to correctly overload + operator

I am trying to define a simple class to work with 2d matrices, called Matrix2D01, and having trouble with the + operator.
I have the += operator which is working fine,
Matrix2D01& Matrix2D01::operator+=(Matrix2D01& mat2) {
int i,j;
for (i=0;i<M;i++)
for (j=0;j<N;j++) mat[i][j]+=mat2[i][j];
return *this;
}
The + operator is defined:
Matrix2D01 Matrix2D01::operator+(Matrix2D01& mat2) {
Matrix2D01 result(1,1);
result=*this;
result+=mat2;
return result;
}
When i try to use the + operator, for example by
mat3=mat1+mat2;
the compiler gives an error:
../MatrixGames.cpp:17:12: error: no match for ‘operator=’ in ‘mat3 = Matrix2D01::operator+(Matrix2D01&)((* & mat2))’
If anyone can tell me what I'm doing wrong it will be greatly appreciated.
Thanks.
I also have a = operator defined,
Matrix2D01& Matrix2D01::operator=(Matrix2D01& mat2) {
if (this==&mat2) return *this;
int i,j;
for (i=0;i<M;i++) {
delete [] mat[i];
}
delete [] mat;
M=mat2.Dimensions()[0];
N=mat2.Dimensions()[1];
mat = new double* [M];
for (i=0;i<M;i++) {
mat[i]=new double [N];
}
for (i=0;i<M;i++)
for (j=0;j<M;j++)
mat[i][j]=mat2[i][j];
return *this;
}
here is a complete test case:
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
using namespace std;
class Matrix2D01 {
protected:
int D;
double **mat;
int M,N;
public:
Matrix2D01(int m,int n);
Matrix2D01(int m,int n,double d);
~Matrix2D01();
vector <int> Dimensions() {vector <int> d(D,M); d[1]=N; return d;}
double* operator[](int i) {return mat[i];}
Matrix2D01& operator=(Matrix2D01& mat2);
Matrix2D01& operator+=(Matrix2D01& mat2);
Matrix2D01 operator+(Matrix2D01& mat2);
};
Matrix2D01::Matrix2D01(int m, int n) {
int i,j;
D=2;
M=m;
N=n;
mat = new double* [M];
for (i=0;i<M;i++) {
mat[i]=new double [N];
}
for (i=0;i<M;i++)
for (j=0;j<M;j++)
mat[i][j]=0;
}
Matrix2D01::Matrix2D01(int m, int n,double d) {
int i,j;
D=2;
M=m;
N=n;
mat = new double* [M];
for (i=0;i<M;i++) {
mat[i]=new double [N];
}
for (i=0;i<M;i++)
for (j=0;j<M;j++)
mat[i][j]=d;
}
Matrix2D01::~Matrix2D01() {
int i;
for (i=0;i<M;i++) {
delete [] mat[i];
}
delete [] mat;
}
Matrix2D01& Matrix2D01::operator=(Matrix2D01& mat2) {
if (this==&mat2) return *this;
int i,j;
for (i=0;i<M;i++) {
delete [] mat[i];
}
delete [] mat;
M=mat2.Dimensions()[0];
N=mat2.Dimensions()[1];
mat = new double* [M];
for (i=0;i<M;i++) {
mat[i]=new double [N];
}
for (i=0;i<M;i++)
for (j=0;j<M;j++)
mat[i][j]=mat2[i][j];
return *this;
}
Matrix2D01& Matrix2D01::operator+=(Matrix2D01& mat2) {
int i,j,M2,N2;
M2=mat2.Dimensions()[0];
N2=mat2.Dimensions()[1];
if ((M!=M2)||(N!=N2)) {
cout<<"error: attempted to add non-matching matrices";
return *this;
}
for (i=0;i<M;i++)
for (j=0;j<N;j++) mat[i][j]+=mat2[i][j];
return *this;
}
Matrix2D01 Matrix2D01::operator+(Matrix2D01& mat2) {
Matrix2D01 result(1,1);
result=*this;
result+=mat2;
return result;
}
int main() {
Matrix2D01 mat1(2,2,1);
Matrix2D01 mat2(2,2,2);
Matrix2D01 mat3(2,2,4);
mat3+=mat1;
mat3=mat1+mat2;
return 1;
}
Herb Sutter advocates the following canonical way to overload operator + (please read GotW, it is really well written and interesting):
Don't include operator + as a member function, instead make it a free function.
Pass one object by value and the other by const reference. This makes it possible to use move operators when you add to a temporary.
Implement in terms of operator +=:
Matrix2D01 operator+(Matrix2D01 a, const Matrix2D01 &b)
{
a += b;
return a;
}
I've posted the code of the Matrix class I had written for an assignment on Discrete Mathematics last Sem
Notice how the assignment operator and the copy constructor are implemented All you need to do is write an assignment operator (I'd recommend that you write a copy constructor as well)
#pragma once
#include <vector>
#include <istream>
#include <ostream>
class Matrix
{
private:
unsigned int rows, cols;
std::vector<std::vector<int> > matrix;
public:
// Creates a Matrix with the given dimensions and sets the default value of all the elements to 0
Matrix(unsigned int rows, unsigned int cols);
// Destructor
~Matrix();
// Converts the relation set into it's adjacency Matrix representation
static Matrix CreateFromRelationSet( std::vector<std::pair<int, int> > relationSet);
// Copy Constructor
Matrix(const Matrix& cSource);
// Assignment Operator
Matrix& operator=(const Matrix& cSource);
// Resets all the elements of the matrix to 0
void Reset();
// Resizes the matrix to the given size
void Resize(unsigned int rows, unsigned int cols);
// Returns the number of rows
unsigned int getRows();
// Returns the number of columns
unsigned int getCols();
// Returns the smallest element from the matrix
int getSmallestElement();
// Returns the greatest element from the matrix
int getGreatestElement();
// Returns true if element is found and sets the row and column
// Returns false if not found
bool getPosition(int element, int* i, int* j);
// Deletes a row from the Matrix
void DeleteRow(unsigned int row);
// Deletes a column from the Matrix
void DeleteColumn(unsigned int col);
// Returns the element at (i,j)
int& operator()(unsigned int i, unsigned j);
// Returns the element at (i,j). Use the () operators instead
int getElementAt(unsigned int i, unsigned j);
friend std::ostream& operator << (std::ostream& out, Matrix& m);
friend std::istream& operator >> (std::istream& in, Matrix& m);
Matrix operator + (Matrix M);
Matrix operator - (Matrix M);
Matrix operator * (Matrix M);
};
// For use with cin & cout
std::ostream& operator << (std::ostream& out, Matrix& m);
std::istream& operator >> (std::istream& in, Matrix& m);
#include "Matrix.h"
Matrix::Matrix(unsigned int rows, unsigned int cols)
{
this->rows = rows;
this->cols = cols;
matrix.resize(rows);
for(std::vector<int>& i: matrix)
i.resize(cols);
Reset();
}
Matrix::~Matrix()
{
}
// local helper function
int findMaxFromSet(std::vector<std::pair<int, int> > set)
{
int maxVal = 0;
for(unsigned int i = 0; i < set.size(); i ++)
{
int p = set[i].first > set[i].second ? set[i].first : set[i].second;
maxVal = maxVal > p ? maxVal : p;
}
return maxVal;
}
Matrix Matrix::CreateFromRelationSet (std::vector<std::pair<int, int> > relationSet)
{
int max = findMaxFromSet(relationSet);
Matrix M(max,max);
M.Reset();
for(auto i = relationSet.begin(); i != relationSet.end(); ++ i)
M(i->first - 1, i->second - 1) = 1;
return M;
}
void Matrix::Reset()
{
for (auto& i: matrix)
for(auto& j: i)
j = 0;
}
void Matrix::Resize(unsigned int rows, unsigned int cols)
{
matrix.resize(rows);
for(auto& i:matrix)
i.resize(cols);
}
unsigned int Matrix::getRows()
{
return rows;
}
unsigned int Matrix::getCols()
{
return cols;
}
Matrix::Matrix(const Matrix& cSource)
{
rows = cSource.rows;
cols = cSource.cols;
matrix = cSource.matrix;
}
bool Matrix::getPosition(int element, int* i, int* j)
{
for(unsigned int ii = 0; ii < getRows(); ii ++)
for(unsigned int jj = 0; jj < getCols(); jj ++)
if(matrix[ii][jj] == element)
{
*i = ii;
*j = jj;
return true;
}
return false;
}
Matrix& Matrix::operator=(const Matrix& cSource)
{
// check for self-assignment
if (this == &cSource)
return *this;
if(this->getRows() < cSource.rows)
{
this->rows = cSource.rows;
this->matrix.resize(cSource.rows);
}
if(this->getCols() < cSource.cols)
{
this->cols = cSource.cols;
for(auto& i:this->matrix)
i.resize(cSource.cols);
}
for(unsigned int i = 0; i < rows; i++)
for(unsigned int j = 0; j < cols; j++)
this->matrix[i][j] = const_cast<Matrix&>(cSource)(i,j);
return *this;
}
std::ostream& operator << (std::ostream& out, Matrix& m)
{
for(auto& i: m.matrix)
{
for(auto& j: i)
out<<j<<'\t';
out<<std::endl;
}
return out;
}
std::istream& operator >> (std::istream& in, Matrix& m)
{
for(auto& i: m.matrix)
for(auto& j: i)
in>>j;
return in;
}
Matrix Matrix::operator + (Matrix op)
{
// Find the rows and cols of the new matrix
unsigned int r = this->getRows() > op.getRows()?this->getRows():op.getRows();
unsigned int c = this->getCols() > op.getCols()?this->getCols():op.getCols();
// Create Matrices
Matrix A = *this;
Matrix B = op;
Matrix R(r,c);
// Assign values
for(unsigned int i = 0; i < A.rows; i++)
for(unsigned int j = 0; j < A.cols; j++)
R(i,j) = A(i,j) + B(i,j);
return R;
}
Matrix Matrix::operator - (Matrix op)
{
// Find the rows and cols of the new matrix
unsigned int r = this->getRows() > op.getRows()?this->getRows():op.getRows();
unsigned int c = this->getCols() > op.getCols()?this->getCols():op.getCols();
// Create Matrices
Matrix A = *this;
Matrix B = op;
Matrix R(r,c);
// Assign values
for(unsigned int i = 0; i < A.rows; i++)
for(unsigned int j = 0; j < A.cols; j++)
R(i,j) = A(i,j) - B(i,j);
return R;
}
Matrix Matrix::operator* (Matrix op)
{
Matrix A = *this;
Matrix B = op;
if(A.getCols() != B.getRows())
throw std::exception("Matrices cannot be multiplied");
Matrix M(A.getRows(), B.getCols());
for(unsigned int i=0 ; i<A.getRows() ; i++)
for(unsigned int j=0 ; j<B.getCols() ; j++)
for(unsigned int k=0 ; k<B.getRows() ; k++)
M(i,j) = M(i,j) + A(i,k) * B(k,j);
return M;
}
int& Matrix::operator()(unsigned int i, unsigned j)
{
return matrix[i][j];
}
int Matrix::getElementAt(unsigned int i, unsigned j)
{
return (*this)(i,j);
}
int Matrix::getSmallestElement()
{
int result = matrix[0][0];
for(auto i:matrix)
for(auto j : i)
if(j < result)
result = j;
return result;
}
int Matrix::getGreatestElement()
{
int result = matrix[0][0];
for(auto i:matrix)
for(auto j : i)
if(j > result)
result = j;
return result;
}
void Matrix::DeleteRow(unsigned int row)
{
matrix.erase(matrix.begin() + row);
rows --;
}
void Matrix::DeleteColumn(unsigned int col)
{
for(auto& i: matrix)
i.erase(i.begin() + col);
cols --;
}
UPDATE:
result=*this;
In your code you are trying to allocate result the value of *this using the assignment operator, but no assignment operator is defined and the compiler doesn't find a match
Writing an assignment operator overload will solve this issue.
And if you have a copy constructor you can do something like:
Matrix2D01 Matrix2D01::operator+(Matrix2D01& mat2) {
Matrix2D01 result = *this;
result+=mat2;
return result;
}
Writing Assignment Operators: http://www.learncpp.com/cpp-tutorial/912-shallow-vs-deep-copying/
And if you're interested in the details - here you go: http://www.icu-project.org/docs/papers/cpp_report/the_anatomy_of_the_assignment_operator.html
Your operands should be const references. Otherwise, you
can't use a temporary to initialize them. (In this case, the
return value of operator+ is a temporary, and thus, cannot be
bound to the non-const reference of operator=.)
If your operator+ is a member, it should be const as well.
After all, it doesn't modify the object it is called on. So:
Matrix2D01& Matrix2D01::operator=( Matrix2D01 const& mat2 );
Matrix2D01& Matrix2D01::operator+=( Matrix2D01 const& mat2 );
Matrix2D01 Matrix2D01::operator+( Matrix2D01 const& mat2 ) const;
Also, your assignment operator is broken. (Think of what will
happen, if one of the allocations fails after you've freed the
original data.) In general, if you have to test for self
assignment, the assignment operator is broken.

Getting right value from an array within a class that behaves like matrix

I've got class, that behaves lika a matrix, and all operations there like + , -, * etc work well.. But when I'm trying to get a single value, then I get weird values like 2.08252e-317 ...
I've realized, that this bug happens only, when I want to create new metrix this way:
const Matrix b = a ;
without const it works well...
this is the declaration of my class:
class Matrix {
public:
Matrix(int x, int y);
~Matrix(void);
Matrix operator+(const Matrix &matrix) const;
Matrix operator-() const;
Matrix operator-(const Matrix &matrix) const;
Matrix operator*(const double x) const;
Matrix operator*(const Matrix &matrix) const;
class Proxy {
Matrix* _a;
const Matrix* _constA;
int _i;
public:
Proxy(Matrix& a, int i) : _a(&a), _i(i) {
}
Proxy(const Matrix& constA, int i) : _constA(&constA), _i(i) {
}
double& operator[](int j) {
return _a->_arrayofarrays[_i][j];
}
};
Proxy operator[](int i) {
return Proxy(*this, i);
}
Proxy operator[](int i) const {
return Proxy(*this, i);
}
// copy constructor
Matrix(const Matrix& other) : _arrayofarrays() {
_arrayofarrays = new double*[other.x ];
for (int i = 0; i != other.x; i++)
_arrayofarrays[i] = new double[other.y];
for (int i = 0; i != other.x; i++)
for (int j = 0; j != other.y; j++)
_arrayofarrays[i][j] = other._arrayofarrays[i][j];
x = other.x;
y = other.y;
}
int x, y;
double** _arrayofarrays;
};
//constructor
Matrix::Matrix(int x, int y) {
_arrayofarrays = new double*[x];
for (int i = 0; i < x; ++i)
_arrayofarrays[i] = new double[y];
this->x = x;
this->y = y;
}
So I create some instance, fill it with values and then, when I want to print value on some index i get those weird values :-/
ie:
Matrix a(3,5);
//fill in the values to a
cout << a[1][1] << endl // prints out some nonsence
Does anybody have any idea, how to rewrite this class to be able to get proper values?