D
D
Doclassif02021-06-15 18:02:18
C++ / C#
Doclassif0, 2021-06-15 18:02:18

No match for 'operator=' c++ error?

Error on c=ab;
The problem with the types of hike.
[Error] no match for 'operator=' (operand types are 'massiv' and 'massiv')
I can't understand what he wants from me) How to fix it, kind people tell me?

#include <stdio.h>
#include <windows.h>
#include <conio.h>
#include <iostream>
#pragma hdrstop

using namespace std;

class massiv
{	
private:
  int mas[5];
  int size=5;
public:
  massiv() 
  {
    for(int i=0;i<size;i++)
      mas[i]=0;
  }
  massiv(int len) 
  {
    if (len<1 || len>10) size=5;
      else size=len;	
    for(int i=0;i<size;i++)
      mas[i]=0;
  }
  massiv(massiv & w) 
  {
    size=w.size;
    for(int i=0;i<size;i++)
      mas[i]=w.mas[i];
  }
  ~massiv()
  {
    
  }
  
  void vvod()
  {
    cout<<"vvedite massiv"<<endl;
    for(int i=0; i<size; i++)
    {
      cout<<i+1<<"= ";
      cin>>mas[i];
    }
  
  } 
  void print()
  {
    for(int i=0; i<size;i++){
      cout<<mas[i]<<" ";
    }
    cout << endl;
  }
  
  massiv operator-(massiv &w)
  {
    int temp;
    massiv c;
    if (size>w.size)
      temp=size;
    else
      temp=w.size;
      c.size=temp;
    for(int i=0;i<temp;i++)
    {
      if(i<size && i<w.size) 
      {
        c.mas[i]=mas[i]-w.mas[i];
      }
  
      if(i<size && i>=w.size) 
      {
        c.mas[i]=mas[i];
      }
  
      if(i>=size && i<w.size) 
      {
        c.mas[i]=-w.mas[i];
      }
    }
    return c;
  }
  
  massiv operator=(massiv &w)
  {
    for(int i=0;i<size;i++)
      mas[i]=w.mas[i];
    return *this;
  }
};

int main()
{
  massiv a,b,c; 
  a.vvod();
  b.vvod();
  cout<<endl;
  a.print();
  b.print();
  cout<<endl;
  c=a-b;<
  a.print();
  cout<<"-"<<endl;
  b.print();
  cout<<"="<<endl;
  c.print();
  return 0;
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
J
jcmvbkbc, 2021-06-15
@Doclassif0

array operator=(array &w)

must be
massiv& operator=(const massiv &w)
const in parameters -- important.

R
res2001, 2021-06-15
@res2001

The assignment operator must return a reference to itself. You get back a copy of the object.

D
DjTolib, 2021-06-16
@DjTolib

const

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question