Ошибка no match for operator

I am a C++ newbie.I tried out my first program here.To my eyes this program is correct.

#include <iostream>

using namespace std;

class mystruct
{
    private:
        int m_a;
        float m_b;

    public:
        mystruct(int x, float y)
        {
                m_a = x;
                m_b = y;
        }

};


int main()
{

        mystruct m = mystruct(5,3.14);

        cout << "my structure " << m << endl;

        return 0;
}

However I am getting sooo many errors.Can’t figure out why?

cout.cpp: In function ‘int main()’:
cout.cpp:26:29: error: no match for ‘operator<<’ in ‘std::operator<< [with _Traits = std::char_traits<char>]((* & std::cout), ((const char*)"my structure ")) << m’
cout.cpp:26:29: note: candidates are:
/usr/include/c++/4.6/ostream:110:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.6/ostream:110:7: note:   no known conversion for argument 1 from ‘mystruct’ to ‘std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&) {aka std::basic_ostream<char>& (*)(std::basic_ostream<char>&)}’
/usr/include/c++/4.6/ostream:119:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ios_type& (*)(std::basic_ostream<_CharT, _Traits>::__ios_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>, std::basic_ostream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
/usr/include/c++/4.6/ostream:119:7: note:   no known conversion for argument 1 from ‘mystruct’ to ‘std::basic_ostream<char>::__ios_type& (*)(std::basic_ostream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}’
/usr/include/c++/4.6/ostream:129:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.6/ostream:129:7: note:   no known conversion for argument 1 from ‘mystruct’ to ‘std::ios_base& (*)(std::ios_base&)’
/usr/include/c++/4.6/ostream:167:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.6/ostream:167:7: note:   no known conversion for argument 1 from ‘mystruct’ to ‘long int’
/usr/include/c++/4.6/ostream:171:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.6/ostream:171:7: note:   no known conversion for argument 1 from ‘mystruct’ to ‘long unsigned int’
/usr/include/c++/4.6/ostream:175:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.6/ostream:175:7: note:   no known conversion for argument 1 from ‘mystruct’ to ‘bool’
/usr/include/c++/4.6/bits/ostream.tcc:93:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.6/bits/ostream.tcc:93:5: note:   no known conversion for argument 1 from ‘mystruct’ to ‘short int’
/usr/include/c++/4.6/ostream:182:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.6/ostream:182:7: note:   no known conversion for argument 1 from ‘mystruct’ to ‘short unsigned int’
/usr/include/c++/4.6/bits/ostream.tcc:107:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.6/bits/ostream.tcc:107:5: note:   no known conversion for argument 1 from ‘mystruct’ to ‘int’
/usr/include/c++/4.6/ostream:193:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.6/ostream:193:7: note:   no known conversion for argument 1 from ‘mystruct’ to ‘unsigned int’
/usr/include/c++/4.6/ostream:202:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.6/ostream:202:7: note:   no known conversion for argument 1 from ‘mystruct’ to ‘long long int’
/usr/include/c++/4.6/ostream:206:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.6/ostream:206:7: note:   no known conversion for argument 1 from ‘mystruct’ to ‘long long unsigned int’
/usr/include/c++/4.6/ostream:211:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.6/ostream:211:7: note:   no known conversion for argument 1 from ‘mystruct’ to ‘double’
/usr/include/c++/4.6/ostream:215:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.6/ostream:215:7: note:   no known conversion for argument 1 from ‘mystruct’ to ‘float’
/usr/include/c++/4.6/ostream:223:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.6/ostream:223:7: note:   no known conversion for argument 1 from ‘mystruct’ to ‘long double’
/usr/include/c++/4.6/ostream:227:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.6/ostream:227:7: note:   no known conversion for argument 1 from ‘mystruct’ to ‘const void*’
/usr/include/c++/4.6/bits/ostream.tcc:121:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
/usr/include/c++/4.6/bits/ostream.tcc:121:5: note:   no known conversion for argument 1 from ‘mystruct’ to ‘std::basic_ostream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}’
/usr/include/c++/4.6/bits/basic_string.h:2693:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/ostream:451:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT)
/usr/include/c++/4.6/ostream:456:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char)
/usr/include/c++/4.6/ostream:462:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char)
/usr/include/c++/4.6/ostream:468:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char)
/usr/include/c++/4.6/ostream:473:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char)
/usr/include/c++/4.6/ostream:493:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const _CharT*)
/usr/include/c++/4.6/bits/ostream.tcc:323:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*)
/usr/include/c++/4.6/ostream:510:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*)
/usr/include/c++/4.6/ostream:523:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*)
/usr/include/c++/4.6/ostream:528:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*)

  • 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

Маринчик

0 / 0 / 3

Регистрация: 12.10.2012

Сообщений: 150

1

18.05.2014, 17:36. Показов 13623. Ответов 4

Метки нет (Все метки)


Студворк — интернет-сервис помощи студентам

во всей программе выбивает только одну ошибку.. надоел этот вопрос но что не так????????
92 no match for ‘operator=’ in ‘P3 = operator<(matr&, matr&)(((matr&)(&P2)))’

C++
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<iostream>/*zagolovochnij fajl s klassami, funktsijami i peremennimi dlja organizacii vvoda-vivoda v jazike programmirovaniya C++*/
/*#include <stdlib.h>*/
using namespace std;/*ob'javlenie prostranstva imen  std.*/
 
class matr
{ public:
  float  *x;
  int n,m;
  public:
  matr(int, int);
  matr (matr& f);
  void vvod();
  void ww();
  friend matr  operator+(matr& ob1, matr& ob2);
  matr&  operator=(matr& ob2);
  ~matr();
};
//==============================================
//konstryktor s parametrami
matr::matr(int _n, int _m)
{    n=_n; m=_m;
     x=new float [n*m];
     puts("n konstryktor ");
}
//==============================================
//konstryktor kopii
matr::matr(matr& f)
  {
  n=f.n;
  m=f.m;
  x=new float [n*m];
 for(int i=0; i<n; i++)
 for(int j=0; j<m; j++)
*(x+i*m+j)=*(f.x+i*f.m+j);
  cout <<" rabotal konstryktor kopii  ";
  }
//==============================================
 
void matr::vvod()
{
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
*(x+i*m+j)=rand() % 101 - 50;
}
//==============================================
 
matr::~matr ()
{
delete[]x;
puts("n rabotal destryktor");
}
//==============================================
 
matr  operator<(matr& P1, matr& P2)
{
matr rez(P1.n, P1.m);
puts("n rez dla + ");
for(int i=0;i<rez.n;i++)
for(int j=0;j<rez.m;j++)
*(rez.x +i*rez.m+j)=*(P1.x+i*P1.m+j)+*(P2.x+i*P2.m+j);
return rez;
}
//==================================
matr& matr::operator=(matr& P2)
{
matr rez(P2.m,P2.n);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
*(x+i*m+j)=*(P2.x+i*P2.m+j);
return * this;
}
//==================================
void matr::ww()
{
for(int i=0;i<n;i++)
{
cout<<"n" ;
for(int j=0;j<m;j++)
cout<< *(x+i*m+j)<<"  "; }
}
//==================================
 
int main()
{
matr P1(3,4);
P1.vvod();
P1.ww();
matr P2(3,4);
P2.vvod();
P2.ww();
matr P3(3,4);
P3=P1<P2;
P3.ww();
}



0



Модератор

Эксперт С++

13320 / 10454 / 6253

Регистрация: 18.12.2011

Сообщений: 27,910

18.05.2014, 18:07

2

VS 2008 компилирует без ошибок.
Может важно, что в функции operator= написано return * this;
а не return *this; (без пробела)



1



0 / 0 / 3

Регистрация: 12.10.2012

Сообщений: 150

18.05.2014, 18:42

 [ТС]

3

а мы в Dev-C++ работаем. и у меня выбивает ошибку((((



0



1500 / 1146 / 165

Регистрация: 05.12.2011

Сообщений: 2,279

18.05.2014, 18:58

4

Лучший ответ Сообщение было отмечено Маринчик как решение

Решение

а зачем у вас конструктор копирования, оператор = и прочие принимают неконстантную ссылку?
в этих функциях передаваемые объекты меняются? нет. значит нужно константную.
в вашем случае идет попытка вызова оператора =, которому передается временный объект (результат оператора <).
неконстантные ссылки не строятся из временных объектов. это запрещено. кстати, студия такую ошибку почему-то
не считает за ошибку.
короче, сделайте все аргументы своих функций константными ссылками, если внутри функций передаваемый
объект не изменяется.



1



zss

Модератор

Эксперт С++

13320 / 10454 / 6253

Регистрация: 18.12.2011

Сообщений: 27,910

18.05.2014, 19:06

5

Лучший ответ Сообщение было отмечено Маринчик как решение

Решение

Ссылки надо сделать константными в равно и в копиконструкторе

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//konstryktor kopii
matr::matr(const matr& f)
  {
  n=f.n;
  m=f.m;
  x=new float [n*m];
 for(int i=0; i<n; i++)
 for(int j=0; j<m; j++)
*(x+i*m+j)=*(f.x+i*f.m+j);
  cout <<" rabotal konstryktor kopii  ";
  }
//==================================
matr& matr::operator=(const matr& P2)
{
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
*(x+i*m+j)=*(P2.x+i*P2.m+j);
return *this;
//  ???????????? что будет, если размерности разные  ???????????
}



1



I’m getting a different error now.

In file included from toml11/toml/types.hpp:8:0,
                 from toml11/toml/parser.hpp:9,
                 from toml11/toml.hpp:36,
                 from toml-test.cpp:1:
toml11/toml/comments.hpp: In member function ‘toml::preserve_comments::iterator toml::preserve_comments::insert(toml::preserve_comments::const_iterator, const string&)’:
toml11/toml/comments.hpp:86:36: error: no matching function for call to ‘std::vector<std::basic_string<char> >::insert(toml::preserve_comments::const_iterator&, const string&)’
         return comments.insert(p, x);

  • Ошибка no iwd files found in main
  • Ошибка no hardware found
  • Ошибка no getpropnames function for undefined
  • Ошибка no function definition vlax ename vla object
  • Ошибка no function definition nil