L
L
LNK2016-05-03 16:30:53
Programming
LNK, 2016-05-03 16:30:53

How to change the values ​​of variables of another class in C++?

In general, the task is as follows, there are 2 classes: the array class and the matrix class. The array class is a class whose field is a one-dimensional array, and the matrix class is a class whose field is a one-dimensional array of array objects. thus, in the matrix class, our field will be a two-dimensional array. I have a problem that I cannot change the values ​​of the elements of a two-dimensional array in the matrix class. Here is the source code:

#include <iostream>
#include <time.h>
#include <iomanip>

using namespace std;

class matrica;

class massiv
{
public:
  int x[5];
  massiv(){
    for (int i = 0; i < 5; i++){
      x[i] = rand() % 21 - 10;
      cout << setw(4) << x[i];
    }
    cout << endl;
  }

  int  operator [](int i)
  {
    return x[i];
  }
  friend matrica;
};



class matrica
{
  
public:
  massiv y[5];
  matrica(){
    cout << y[0][0];//все прекрасно выводится
                /*но если попробовать сделать y[0][0]+=1; то выводится ошибка "выражение должно быть допустимым для изменения левосторонним значением"*/
  }

  int  operator [](int i)
  {
    return y[i][0]; /*совершенно непонятно, какую роль здесь играет [0], и почему не работает с просто return y[i]... при изменении [0] на любое другое число, результат не меняется...*/
  }
};

void main(){
  srand(time(NULL));
  matrica example;
  system("pause");
}

how can you change the values ​​of a particular element of this two-dimensional array from the matrix class? I also wanted to fill it from the matrix class

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Movchan, 2016-05-03
@NikHaker

First :
If you write in C++ and use C header files, it's common to call them ctime, not time.h
Second :
For pseudo-random numbers in C++, use the random header file :

#include <random>
...
std::random_device rd;
std::cout << rd() % 10;

Third :
If you need an array, use an array. What you wrote is a very strange crutch.
If you need to fill an array with random numbers, write a function:
void fill_with_random(int* arr, size_t n)
{
    random_device rand;
    for (size_t i = 0; i < n; ++i)
    {
        arr[i] = rand() % 21 - 10; 
    }
}

Fourth :
cout << y[0][0]; //все прекрасно выводится
/*но если попробовать сделать y[0][0]+=1; то выводится ошибка "выражение должно быть допустимым для изменения левосторонним значением"*/

Naturally, this happens because in your class (which is not needed at all) you return a number where a reference is expected:
int operator [](int i)
{
    return x[i];
}

Replaced by:
int& operator [](int i)
{
    return x[i];
}

Fifth :
int  operator [](int i)
{
    return y[i][0]; /*совершенно непонятно, какую роль здесь играет [0], и почему не работает с просто return y[i]... при изменении [0] на любое другое число, результат не меняется...*/
}

It is not clear because it should not be there. operator[] should give access to a string (i.e. an array). And already the second operator[] will refer to the array and return a reference to the element.
massiv& operator [](int i)
{
    return y[i];
}

Sixth :
If you need a matrix, use a two-dimensional array and don't reinvent the wheel. If a class is still needed, for example, if you need to add methods for working with a matrix, you can write such a class using a two-dimensional array:
class Matrix
{
public:
    Matrix() {}
    int* operator[] (size_t i)
    {
        return &arr[i][0]; // Указатель на первый элемент i-ой строки.
    }
private:
    int arr[5][5];
}

Seventh :
Classes are usually called with a capital letter. And please don't call them translit.
Array - Array
Matrix - Matrix

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question