No match for operator ошибка

Here is the Rational class that i’ve been working on:

rational.h

#include<iostream>

using namespace std;

#ifndef RATIONAL_H
#define RATIONAL_H

class Rational
{
  int numerator,denominator;
  public:
  // the various constructors
  Rational();
  Rational(int);
  Rational(int,int);

  //member functions
  int get_numerator()const{return numerator;}
  int get_denominator()const{return denominator;}

  // overloaded operators
  // relational operators
  bool operator==(const Rational&)const;
  bool operator<(const Rational&)const;
  bool operator<=(const Rational&)const;
  bool operator>(const Rational&)const;
  bool operator>=(const Rational&)const;

  //arithmetic operators
  Rational operator+(const Rational&);
  Rational operator-(const Rational&);
  Rational operator*(const Rational&);
  Rational operator/(const Rational&);

  //output operator
  friend ostream& operator<<(ostream&, const Rational&);
};
#endif //RATIONAL_H

rational.cpp

#include "rational.h"

// implementation ofthe various constructors
Rational::Rational()
  :numerator(0),denominator(1){}

Rational::Rational(int number)
  :numerator(number),denominator(1){}

Rational::Rational(int n,int d)
  :numerator(n),denominator(d)
{
  if(denominator == 0) denominator = 1;
  if(denominator < 0)
  {
    numerator *= -1;
    denominator *= -1;
  }
}

// implementation of overloaded operators
bool Rational::operator==(const Rational& rhs)const
{
  if( numerator * rhs.get_denominator() == denominator * rhs.get_numerator() )
  {
    return true;
  }
  else return false;
}

bool Rational::operator<(const Rational& rhs)const
{
  if( numerator * rhs.get_denominator() < denominator * rhs.get_numerator() )
  {
    return true;
  }
  else return false;
}

bool Rational::operator<=(const Rational& rhs)const
{
  return operator==(rhs) || operator<(rhs);
}

bool Rational::operator>(const Rational& rhs)const
{
  return !operator<(rhs);
}

bool Rational::operator>=(const Rational& rhs)const
{
  return operator==(rhs) || operator>(rhs);
}

//arithmetic operators
Rational Rational::operator+(const Rational& rhs)
{
  return Rational( (numerator * rhs.get_denominator() + denominator*rhs.get_numerator()), (denominator * rhs.get_denominator()) );
}

Rational Rational::operator-(const Rational& rhs)
{
  //reuse of the + operator for substraction
  return operator+(Rational(-1*rhs.get_numerator(),rhs.get_denominator()));
}

Rational Rational::operator*(const Rational& rhs)
{
  return Rational(numerator * rhs.get_numerator(), denominator * rhs.get_denominator());
}

Rational Rational::operator/(const Rational& rhs)
{
  //reuse of the * operator as division is the inverse of multiplication
  return operator*(Rational(rhs.get_denominator(),rhs.get_numerator()));
}

// friend output operator
ostream& operator<<(ostream& os, const Rational& r)
{
   os<<r.get_numerator()<<"/"<<r.get_denominator();
   return os;
}

and the driver for the program driver.cpp
#include "rational.h"

int main()
{
  Rational r1(),r2(3),r3(11,3),tmp;
  cout<<r1+r2<<endl;
  cout<<r2<<endl;
  cout<<r2-r3<<endl;
  cout<<r2*r3<<endl;
  cout<<r1/r3;

  return 0;
}

Here’s the error that i get when trying to compile it.

driver.cpp: In function ‘int main()’:
driver.cpp:6:12: error: no match for ‘operator+’ in ‘r1 + r2’
driver.cpp:6:12: note: candidates are:
/usr/include/c++/4.6/bits/stl_iterator.h:327:5: note: template<class _Iterator> std::reverse_iterator<_Iterator> std::operator+(typename std::reverse_iterator<_Iterator>::difference_type, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/basic_string.h:2306:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.tcc:694:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.tcc:710:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(_CharT, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.h:2343:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
/usr/include/c++/4.6/bits/basic_string.h:2359:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, _CharT)
driver.cpp:10:12: error: no match for ‘operator/’ in ‘r1 / r3’

When I comment the lines where I use the operator+ and operator/, then the code works.
This baffles me as I have implemented the operator- using operator+ and similarly the operator/ using operator*.
So if one of them works, I’d figure that the other would work too.
Can someone please explain what I have done wrong here?

Edit
I get even more errors when I use the operator==

rational.cpp:22:6: error: prototype for ‘bool Rational::operator==(const Rational&)’ does not match any in class ‘Rational’
rational.h:23:8: error: candidate is: bool Rational::operator==(Rational)
rational.cpp:31:6: error: prototype for ‘bool Rational::operator<(const Rational&) const’ does not match any in class ‘Rational’
rational.h:24:8: error: candidate is: bool Rational::operator<(Rational)
driver.cpp: In function ‘int main()’:
driver.cpp:11:10: error: no match for ‘operator==’ in ‘r1 == tmp’
driver.cpp:11:10: note: candidates are:
/usr/include/c++/4.6/bits/postypes.h:218:5: note: template<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
/usr/include/c++/4.6/bits/stl_pair.h:201:5: note: template<class _T1, class _T2> bool std::operator==(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/include/c++/4.6/bits/stl_iterator.h:285:5: note: template<class _Iterator> bool std::operator==(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/stl_iterator.h:335:5: note: template<class _IteratorL, class _IteratorR> bool std::operator==(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
/usr/include/c++/4.6/bits/allocator.h:122:5: note: template<class _T1, class _T2> bool std::operator==(const std::allocator<_T1>&, const std::allocator<_T2>&)
/usr/include/c++/4.6/bits/allocator.h:127:5: note: template<class _Tp> bool std::operator==(const std::allocator<_Tp1>&, const std::allocator<_Tp1>&)
/usr/include/c++/4.6/bits/basic_string.h:2427:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.h:2434:5: note: template<class _CharT> typename __gnu_cxx::__enable_if<std::__is_char<_Tp>::__value, bool>::__type std::operator==(const std::basic_string<_CharT>&, const std::basic_string<_CharT>&)
/usr/include/c++/4.6/bits/basic_string.h:2448:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.h:2460:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
/usr/include/c++/4.6/bits/streambuf_iterator.h:194:5: note: template<class _CharT, class _Traits> bool std::operator==(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&)

What do these cryptic messages mean?

  

The 'No match for operator<<' error in C++ usually occurs when you try to use the `<<` operator with an object for which the operator<< is not defined or overloaded. In this guide, we will discuss the causes of this error and provide step-by-step solutions to resolve it. 

## Table of Contents

- [Understanding the operator<< in C++](#understanding-the-operator-in-c)
- [Common causes of the 'No match for operator<<' error](#common-causes-of-the-no-match-for-operator-error)
- [Step-by-step guide to resolving the error](#step-by-step-guide-to-resolving-the-error)
- [FAQ](#faq)

<a name="understanding-the-operator-in-c"></a>
## Understanding the operator<< in C++

In C++, the `<<` operator is used for various purposes, such as:

- As a bitwise left shift operator for integral types
- As an insertion operator for streams (like `std::ostream`)

For example, when using `std::cout` to print values to the console, you are actually using the overloaded `<<` operator for `std::ostream`.

```cpp
#include <iostream>

int main() {
    int num = 42;
    std::cout << "The answer is: " << num << std::endl;
    return 0;
}

Common causes of the ‘No match for operator<<‘ error

Here are some common causes of the ‘No match for operator<<‘ error:

  1. Attempting to use the << operator with a custom class or struct without overloading it
  2. Forgetting to include the required header files
  3. Using the wrong namespace

Step-by-step guide to resolving the error

Step 1: Overload the operator<< for your custom class or struct

To resolve the ‘No match for operator<<‘ error, you need to overload the << operator for your custom class or struct. Here’s an example:

#include <iostream>

class MyClass {
public:
    int value;

    MyClass(int v) : value(v) {}
};

std::ostream& operator<<(std::ostream& os, const MyClass& obj) {
    os << "MyClass value: " << obj.value;
    return os;
}

int main() {
    MyClass obj(42);
    std::cout << obj << std::endl;
    return 0;
}

Ensure that you have included the necessary header files, such as <iostream> for using std::cout and the << operator for streams.

Step 3: Ensure that you are using the correct namespace

Make sure you are using the correct namespace, such as std for standard library functions and classes:

using namespace std;

FAQ

Q1: Can I overload the operator<< for built-in types like int or float?

No, you cannot overload the << operator for built-in types like int or float. The standard library already provides overloads for these types.

Q2: How do I overload the operator<< for a template class?

To overload the << operator for a template class, you can define the operator as a template function:

template<typename T>
std::ostream& operator<<(std::ostream& os, const MyTemplateClass<T>& obj) {
    os << "MyTemplateClass value: " << obj.value;
    return os;
}

Q3: What if I want to use the operator<< with a pointer to an object?

If you want to use the << operator with a pointer to an object, you need to overload the operator for pointers:

std::ostream& operator<<(std::ostream& os, const MyClass* obj) {
    os << "MyClass value: " << obj->value;
    return os;
}

Q4: Can I overload the operator<< as a member function of my class?

No, you cannot overload the << operator as a member function of your class. The left-hand operand of the << operator is a stream (like std::ostream), and you cannot add member functions to stream classes.

Q5: Can I use the operator<< with other stream classes like std::ofstream?

Yes, you can use the << operator with other stream classes like std::ofstream. You just need to include the appropriate header file (like <fstream> for std::ofstream) and make sure your overloaded operator works with the base std::ostream class.

  • C++ Operators Overloading
  • C++ I/O Streams
  • C++ Namespaces
    «`
  • Forum
  • Beginners
  • no match for operator>>

no match for operator>>

Hi, so I’m a total beginner and am having trouble trying to overload the insertion operator. When I try it tells me «error: no match for ‘operator>>’ (operand types are ‘std::istream’ {aka ‘std::basic_istream’} and ‘int’)»

Here is the header:

1
2
3
4
5
6
7
8
9
10
11
12
  #include <iostream> 
using namespace std;

class myArray {
    public:
    myArray(int len = 0);
    int operator[](int i) { return a[i]; }
    friend istream& operator>> (istream &in,  myArray &x);
    private:
    int arrayLen;
    int a[];
};

And this is the implementation file:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream> 
#include "myArray.h"
using namespace std;

myArray::myArray(int len){
    arrayLen = len;
}

myArray::istream& operator>> (istream &in,  myArray &x)
{
    in >> x.a;
    return in;
}

I assume that I have a type mismatch, but I guess I’m a little thick, because I don’t see it.

What do YOU think line 11 is doing in the 2nd one?
How big do you think that A array is?

you cannot cin an array. you have to loop and read one by one.
and C style arrays need a fixed size at compile time; the only way around this is a pointer of some sort (whether done for you or not).

edit, misread what you did, the >> is fine apart from trying to read and write an array.

Last edited on

There are three main issues with your code.

The first issue is that your array a is not specified as having any size. The size of arrays, whether in a class or not, must be known at compile time.

The second issue is second snippet, line 9: myArray::istream& is not a type. You meant to just write istream&.

The third issue is second snippet, line 11: You are calling >> on an array (a). Presumably you meant to call something more like:

1
2
in >> x.a[arrayLen];
arrayLen++;

You should also be thinking about bounds checking, e.g. (if arrayLen == maxSize) { don’t add to array }. This is assuming you have some notion of «MaxSize» (see first issue).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>

using namespace std;

constexpr int MaxSize = 1000;

class myArray {
public:
    myArray(int len = 0);
    int operator[](int i) const { return array[i]; }
    friend istream& operator>> (istream &in,  myArray &x);
    
    //private: // making public for demonstration
    int arrayLen;
    int array[MaxSize];
};

myArray::myArray(int len)
: arrayLen(len) {

}

istream& operator>> (istream &in,  myArray &arr)
{
    if (arr.arrayLen < MaxSize)
    {
        in >> arr.array[arr.arrayLen];
        arr.arrayLen++;
    }

    return in;
}

int main()
{
    myArray myArr;
    std::cin >> myArr >> myArr >> myArr;

    for (int i = 0; i < myArr.arrayLen; i++)
    {
        std::cout << myArr.array[i] << 'n';
    }
}

Last edited on

So does that mean that I could do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream> 
#include "myArray.h"
using namespace std;


myArray::myArray(int len)
{
arrayLen=len;
}

istream& operator>> (istream &in,  myArray &arr)
{
    if (arr.arrayLen < MaxSize)
    {
        in >> arr.array[arr.arrayLen];
        arr.arrayLen++;
    }

    return in;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream> 
using namespace std;

constexpr int MaxSize = 1000;

class myArray {
    public:
    myArray(int len = 0);
    int operator[](int i) { return array[i]; }
    friend istream& operator>> (istream &in,  myArray &x);
    private:
    int arrayLen;
    int array[MaxSize];
};

Wait, no, that still doesn’t work… Okay, I still seem to be misunderstanding something.

What does «not work» mean?

The same error comes up about «no match for ‘operator>>’ (operand types are ‘std::istream’ {aka ‘std::basic_istream’} and ‘int’)»

Perhaps something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

constexpr size_t MaxSize {1000};

class myArray {
public:
	myArray() {}
	size_t size() const { return arrayLen; }

	int operator[](size_t i) { return array[i]; }
	const int operator[](size_t i) const { return array[i]; }
	friend std::istream& operator>> (std::istream& in, myArray& x);

private:
	size_t arrayLen {};
	int array[MaxSize] {};
};

std::istream& operator>> (std::istream& in, myArray& arr) {
	if (arr.arrayLen < MaxSize)
		in >> arr.array[arr.arrayLen++];

	return in;
}

int main() {
	myArray myArr;

	std::cin >> myArr >> myArr >> myArr;

	for (size_t i {}; i < myArr.size(); ++i)
		std::cout << myArr[i] << 'n';
}

person5273, your code that you have shown does not have problems, assuming it’s being successfully compiled/linked by however you are building it. I was able to copy and paste it into cpp.sh as one file and compile it (after removing the #include «myArray.h»), after adding a main function. So the problem exists in part of the code you are not showing.

The array size is specified at compile time.

Topic archived. No new replies allowed.

No match for operator c++ is an error that typically occurs when you’re trying to print something that’s not pre-defined in this programming language. However, “no match for operator c++” is just one part of the error, and to understand what’s really causing it and how you can solve it, you need to look at the whole error, including what it says in the parentheses.No Match for Operator C

In this article, we’ll take a look at the different instances of this error, when they show up, and what you can do to solve the problem.

Contents

  • What Causes No Match for Operator C++? All Common Reasons
    • – Error: No Match for ‘Operator<<’ in ‘Std::Operator<<[with_traits = Std::char_traits<Char>]
    • – No Match for ‘Operator<<’ (Operand Types Are ‘Std::Ostream {aka Std::basic_ostream}’ and ‘Std::Vector’)
    • – No Match for Operator = Iterator C++ and Other Reasons
  • Getting Rid of the Error: No Match for Operator C++
    • – Adding Operator <<
  • FAQs
    • 1. What Does Operator Overloading Mean In C++?
    • 2. Why Does No Match For Operator C++ Occur When Printing Variables?
  • Conclusion

What Causes No Match for Operator C++? All Common Reasons

The causes of no match for operator c++ error differ from one case to the other. If it says no match for operator << in std::operator, that means the compiler doesn’t know how you want your object or structure to be printed. Moreover,, cout << doesn’t work for all types.

– Error: No Match for ‘Operator<<’ in ‘Std::Operator<<[with_traits = Std::char_traits<Char>]

To completely understand the error, we’ll have to look at it on a case-by-case basis. The first case is where the error says there’s no match for operator<< in std::operator.

Here, you see the error because the compiler doesn’t really know how you want the structure to be printed, and it can’t find the matching overload for the operator <<. The rest of the error message is just the compiler listing the different operator<< overloads that were not a match. The operator provided by the standard library doesn’t know what to do with the user-defined type mystruct, and it only works with pre-defined data types.

– No Match for ‘Operator<<’ (Operand Types Are ‘Std::Ostream {aka Std::basic_ostream}’ and ‘Std::Vector’)

Operators like << and + are basically like functions. Just like classes can override member functions in order to change their behavior, you can do the same for operators. This is known as operator overloading. And you’ve probably been using it in many places without realizing it. For instance, “<<” in cout << and cin >> is an operator.No Match for Operator C Causes

Cout << can work with many types, but in some cases, it can throw an error. For instance, the following code snippet will produce the error: no match for ‘operator<<’ (operand types are…):

#include <iostream>

#include <vector>

using std::cout; using std::endl;

using std::vector;

int main() {

vector<int> vec = {1, 2, 3};

cout << vec << endl;

return 0;

Sometimes, you might also see this error when trying to print the return of the vector with std::cout.

– No Match for Operator = Iterator C++ and Other Reasons

When you see this error, it typically means that you’re calling ‘begin’ on a copyin. The latter is a const and if you call begin on this, it’ll give you an immutable iterator, which is what causes the error.

  • There can be a few other reasons for this error. These include the following:
  • In some cases, you might also see this error if you try to use an object that isn’t a variable, like a timer object in Arduino. So if you try and do normal variable operations with this object like timer = timer + pirConstTimer, you’ll come across the error “no match for operator.”
  • The problem can also be the type of array or input variable that you defined. If they’re of the same or of compatible types, it’s necessary to also show their definition. But if they’re of the same primitive types like int, then there’s a problem. There’s a high chance that the types aren’t built-in and didn’t overload the < and > operators.
  • Another cause could be you trying to print a void function or statement. If the function is void, i.e., it doesn’t return anything, you can’t print something. And if you want to print using a cout function, then the function should return something like a double, int, string, etc.

Getting Rid of the Error: No Match for Operator C++

To solve the error: no match for operator C++, you need to explicitly tell it by providing a function called operator<< that basically takes parameters: ostream and your structure type. In other words, you need to perform operator overloading so that operator << can take the user-defined data type.



– Adding Operator <<

In addition to the small fixes mentioned above, there are a few more things you can try out, but the most important thing is to perform operator overloading. To do so, you need to add something like this to your code snippet:

friend ostream& operator << (ostream& os, const mystruct& m)

In this line of code, the operator<< is a function that takes two parameters: ostream and your type, which, in this case, is a const called mystruct. Always remember that the first parameter will always be the reference to the ostream object while the second parameter will be the user-defined object, which is also why the operator function must be implemented as a non-member function.Adding Operator

Typically, it is implemented as a friend function, since it allows them to get access to the private attributes of a user-defined type. Sometimes, this is the only way you can access them, especially if the user-defined type has not exposed the attributes through getters.

So, continuing with the example above and adding operator overloading to it, this is what the final code will then look like:

#include <iostream>

#include <vector>

using std::cout; using std::endl;

using std::vector;

std::ostream& operator<<(std::ostream& os, const vector& vec) {

for (vector::const_iterator it = vec.cbegin();

it != vec.cend(); ++it)

{

os << *it << ‘ ‘;

}

return os;

}

int main() {

const vector<int> vec = {1, 2, 3};

cout << vec << endl;

return 0;

FAQs

1. What Does Operator Overloading Mean In C++?

In C++, you can provide operators with a special meaning for a certain data type and this is known as operator overloading and is compile-time polymorphism. C++ allows the user to change operators such that they work for user-defined classes.

The basic idea is to give special meaning to an existing C++ operator without modifying its original meaning. For instance, you can overload the ‘+’ operator in a string class so that you can concatenate (or add) two strings just by using +. Similarly, you can overload arithmetic operators in other classes like big integers, fractional numbers, and complex numbers.

2. Why Does No Match For Operator C++ Occur When Printing Variables?

You see no match for operator error when you try to print the variables present in an object using cout. The object is a collection of variables and if you want to print or display the things like the methods inside the object, declaring a getter is mandatory.

Conclusion

Now that you’ve read through the whole article, we’re sure you understand why you see the error ‘no match for operator’ when using c++ and how you can go about solving it. Let’s see the key points that we discussed in this article so that you know how you can avoid the error in the first place:

  • The cause of the error differs depending on what the error message says in the parentheses after “no match for operator.”
  • It could be because the compiler can’t find the matching overload for the operator << or you’re trying to print the return of a vector with std::cout.
  • The error might also appear when you use cout to print variables in an object, when using an object that isn’t a variable, or when printing a void statement.
  • In most cases, you should be able to solve the problem by operator overloading.
  • Operator overloading involves giving special meaning to an operator so that it works for user-defined classes.

After reading our explanation of the error, its cause, and how you can solve it, we’re sure that you know what you need to do next to make sure you don’t see the error anymore!

  • Author
  • Recent Posts

Position is Everything

Your Go-To Resource for Learn & Build: CSS,JavaScript,HTML,PHP,C++ and MYSQL. Meet The Team

Position is Everything

Ошибка на c=a-b;
Проблема с типами походу.
[Error] no match for ‘operator=’ (operand types are ‘massiv’ and ‘massiv’)
Не могу понять что он от меня хочет) Как исправить, подскажите добрые люди?

#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;
}

Понравилась статья? Поделить с друзьями:
  • No mans sky ошибка подключения здание
  • Nissan qashqai ошибки на приборной панели
  • No mans sky ошибка драйвера vulkan
  • Nissan qashqai ошибка p1706
  • No mans sky ошибка msvcr120 dll