D
D
Durilka962020-12-03 08:33:05
C++ / C#
Durilka96, 2020-12-03 08:33:05

How to fix unary operator error?

tell me what the error means and how to fix it? [bcc32c Error] File1.cpp(152): invalid argument type 'DinMass' to unary expression

#include <cstdlib>
#include <conio.h>
#include <cstdio>
using namespace std;

class DinMass {
  int size, *mas;

public:
  void info(); // Информация об авторе

  DinMass(int p_size = 0, int N = 0) { // Конструктор
    if (p_size) {
      size = p_size;
      mas = new int[size];
      if (N) {

        for (int i = 0; i < size; i++) {
          mas[i] = random(N) - random(N);
        }
      }
      else {
        for (int i = 0; i < size; i++) {
          mas[i] = 0;
        }
      }
    }
    else {
      mas = NULL;
      size = 0;
    }
  }
  void Show(); // Вывод массива
  int *GetMass ();
  //--------------------------------------
  int *operator - (DinMass &);
  int GetSize();
  DinMass& operator = (const DinMass & m) {
    if (&m != this) {
      if (mas) {
        delete[]mas;
      }
      if (m.mas) {
        size = m.size;
        mas = new int [size];
        for (int i = 0; i < size; i++) {
          mas[i] = m.mas[i];
        }
      }
    }
    return *this;
  }
  friend int operator > (DinMass &, DinMass &);

  ~DinMass() { // Деструктор

  }
};

int *DinMass::GetMass () {
return mas;
}



int DinMass::GetSize() {
  return size;
}

int operator > (DinMass &m, DinMass &m1) {
  int *p1 = m.GetMass();
  int *p2 = m1.GetMass();
  int sum1 = 0, sum2 = 0;
  for (int i = 0; i < m.GetSize(); i++) {
  sum1 += p1[i];
  }
  for (int i = 0; i < m1.GetSize(); i++) {
  sum2 += p2[i];
  }
  if (sum1 > sum2) {
  return 1;
  }
  else {
  return 0;}
}

void DinMass::info() {
  printf("Выполнил\n");
  // Информация об авторе
}


//-------------------------------------------------
int *DinMass::operator - (DinMass& m) {
   int *p = m.GetMass();
   int *arr = new int [m.size];
   for (int i = 0; i < m.size; i++) {
   if (p[i] > 0) {
   arr [i] = p[i]*(-1);
   }
   else {
     arr [i] = p[i];
   }
   }

   return arr;
}


void DinMass::Show() {
  for (int i = 0; i < size; i++) {
    printf("%i ", mas[i]);
  }
  printf("\n");
}

double* operator / (DinMass& m1, DinMass &m2) {
int size = 0;
int *p1 = m1.GetMass();
int *p2 = m2.GetMass();
if (m1.GetSize()  < m2.GetSize()) {
size = m1.GetSize();
}
else {
  size = m2.GetSize();
}
double *array = new double [size];
for (int i = 0; i < size; i++) {
array [i] = (double)p2[i]/(double)p1[i];
}
return array;
}

int main() {
  system("chcp 1251");
  randomize();
  printf("Введите кол-во элементов массива k, диапозон значений x\n");
  int k = 10;
  int x = 100;
//  printf("k=");
//  scanf("%i", &k);
//  printf("x=");
//  scanf("%i", &x);
  system("cls");
  DinMass m1(10, 100), m2(k, x), m3(5, 0);
  m1.info();
  m1.Show();
  m2.Show();
  m3.Show();

  int *p = -m1;

   if (m1 > m2) {
    printf("\nCумма элементов 1-го массива больше чем 2-го\n" );
   }
   else {
     printf("\nСумма элементов 1-го массива меньше чем 2-го\n");
   }

   printf("\nВывод глобальной функции\n");
   printf ("Поэлиментное деление элементов массива: ");
   double *q = m1/m2;
   for (int i = 0; i < 10; i++) {
   printf ("\n%.02f",q [i]);
   }


  printf("\n");

  system("pause");
}

you need to print the object m1 with the - operator, which makes all the numbers in the array negative

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nicholas, 2020-12-28
@romancelover

In C++, a unary operator X is defined as either operatorX with no parameters inside the class, or as a function operatorX with one parameter outside the class. Move operator- outside the class, and add friend, just like you did for operator>.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question