-8

i tried to compile my program in C++, but g++ see a lot of errors with operator=, in VS compile without any warning. Please help.

Code What g++ speak

edit. Definition of operator=: Matrix &Matrix::operator=(Matrix &mat){

delete [] tab;
hei = mat.hei;
tab = new Wektor[hei];
for (int i = 0; i < hei; i++){

    tab[i] = mat.Tab()[i];
}
return *this;

}

Definition of function with which is problem: void PlusMat(Matrix *lista, int imat){

Matrix add;
if (imat < 2){
    cout << "At least 2 matricies!";
    return;
}

if ( ((((lista[0]).Tab()[0]).Len()) != (((lista[1]).Tab()[0]).Len())) || (((lista[0]).Hei()) != ((lista[1]).Hei()))){
    cout << "Matrices must have the same dimensions!";
    return;
} 
cout << lista[0]; 
cout << " + \n";
cout << lista[1];
cout << " = \n";
add = ((lista[0]) + (lista[1]));
//  ^ problem
cout << add;

}

edit 2:

I correct this function, but now is another problem:

Wektor &Wektor::operator=(const Wektor &wek) {
delete [] tab;
len = wek.len;
tab = new int[len];
for (int i = 0; i < len; i++)
    tab[i] = wek.Tab()[i];
//           ^ ^            
return *this;

}

the object has type qualifiers that are not compatible with the member function object type is: const Wektor btw i'm sorry, but this is my first post here.

user3609885
  • 66
  • 1
  • 8
  • 4
    Don't expect us to follow links. Post a minimal code example into your question, along with the error messages you're seeing. – Oliver Charlesworth May 06 '14 at 21:13
  • 1
    this site is to help anyone who might benefit from resolving your problem, not just you. So we require that you post your code and errors here. External links can become invalidated so questions/answers with links only are not useful to anyone but you. – bolov May 06 '14 at 21:16
  • `operator=()` should take `const Matrix&`: `operator=(const Matrix& _other)`. And yes, you should post code examples right here due to the reasons, explained by @bolov. Hint: you may edit your answer. Indent code chunks with 4 leading spaces – user3159253 May 06 '14 at 21:16

1 Answers1

2

There are quite a few problems, but the one being shown is that your code has

Wektor &operator=(Wektor &wek);

but the operations return a Wektor by value.

The compiler cannot transform a Wektor in a Wektor& because the result is a temporary and the parameter is a non-const reference.

Changing the parameter of assignment to

Wektor &operator=(const Wektor &wek);

will solve this issue (but like said before this code has quite a few other problems).

If you don't understand why a temporary cannot be converted (bound) to a non-const reference then probably you should read first a bit more about C++ rather that writing code and trying to compile it. Just pick a good book and read it cover-to-cover first, things will be a thousand times easier.

C++ is not a language to learn by experimentation for quite a few reasons, no matter how smart you are (actually being too smart can worsen the situation because sometimes the correct answer is just plain illogical, for historical reasons).

Community
  • 1
  • 1
6502
  • 112,025
  • 15
  • 165
  • 265