why is this "not assignable? "

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 6329 Views - Last Post: 15 August 2012 - 12:30 PM Rate Topic: -----

#1 healix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 29-May 11

why is this "not assignable? "

Posted 14 August 2012 - 07:31 PM

trying to overload += and am getting the error that the expression is not assignable. I figured I'd post only the problem code to make it easier but if needed I will post all code

the implementation file


complex complex::operator+= (complex& param)
{        
    getReal() += param.getReal();
    getImag() += param.getImag();
    
    return;
    

}



the .h declaration


complex operator+= (complex& param);



Is This A Good Question/Topic? 0
  • +

Replies To: why is this "not assignable? "

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,534
  • Joined: 05-May 05

Re: why is this "not assignable? "

Posted 14 August 2012 - 07:41 PM

getReal() += param.getReal();


getReal() is a rvalue, it returns a value, and is not assignable. When you use the += operator, the left hand side of the expression must something that can take a value (e.g. an identifier).

Complex& Complex::operator+=(Complex& c) {
        real = real + c.getReal();
        img = img + c.getImg();
        return *this;
}


This post has been edited by blackcompe: 14 August 2012 - 08:49 PM

Was This Post Helpful? 1
  • +
  • -

#3 healix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 29-May 11

Re: why is this "not assignable? "

Posted 14 August 2012 - 08:16 PM

View Postblackcompe, on 14 August 2012 - 07:41 PM, said:

getReal() += param.getReal();


getReal() is a rvalue, it returns a value, and is not assignable. When you use the += operator, the left hand side of the expression must something that can take a value (e.g. an identifier).

complex complex::operator+= (complex& param)
{        
    setReal(getReal() + param.getReal());
    setImag(getImag() + param.getImag());
    return this;
}



I don't have a setReal and setImag function. if I'm returning 'this' then shouldn't I be able to just call getReal()

also I'm overloading for use with two objects

This post has been edited by healix: 14 August 2012 - 08:27 PM

Was This Post Helpful? 0
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,534
  • Joined: 05-May 05

Re: why is this "not assignable? "

Posted 14 August 2012 - 08:48 PM

Actually I made a mistake in the code. I've updated my previous post.

Quote

if I'm returning 'this' then shouldn't I be able to just call getReal()


I don't understand what you're saying.

This post has been edited by blackcompe: 14 August 2012 - 08:51 PM

Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1076
  • View blog
  • Posts: 4,536
  • Joined: 09-June 09

Re: why is this "not assignable? "

Posted 14 August 2012 - 08:51 PM

getReal returns a rvalue like said before, it would be similar to trying to trying to do something like this
5 = 10 //doesn't make much sense right?



A solution would be to return references from getReal and getImag, but then you sacrifice encapsulation

example

int &getReal { return real; }
int &getImag {return imag; }

Complex& operator+=(Complex& c) {
   getReal() += c.getReal();
   getImag() += c.getImag();
   return *this;
}



This post has been edited by jjl: 14 August 2012 - 08:52 PM

Was This Post Helpful? 1
  • +
  • -

#6 healix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 29-May 11

Re: why is this "not assignable? "

Posted 14 August 2012 - 08:53 PM

View Postblackcompe, on 14 August 2012 - 08:48 PM, said:

Actually I made a mistake in the code. I've updated my previous post.

Quote

if I'm returning 'this' then shouldn't I be able to just call getReal()


I don't understand what you're saying.


ok that works in the implementation file but when I test in main:


    complex a(8,5);
    complex b(5,4);

     cout << a += b;



I get this error: 'no viable overloaded +=
Was This Post Helpful? 0
  • +
  • -

#7 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,534
  • Joined: 05-May 05

Re: why is this "not assignable? "

Posted 14 August 2012 - 08:56 PM

Quote

ok that works in the implementation file but when I test in main:


Well, it wasn't meant to be taken verbatim, but if it works, great.

Quote

I get this error: 'no viable overloaded +=


If you try to apply the extraction operator to a type who hasn't specified it's behavior, then the compiler doesn't know what to do.

friend ostream& operator<< (ostream &out, Complex& c) {
        out << c.getReal() << " + " << c.getImg() << "i";
}



You need to define the getters of course. I'd advise you take a look through the C++ tutorials.

This post has been edited by blackcompe: 14 August 2012 - 08:58 PM

Was This Post Helpful? 0
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1076
  • View blog
  • Posts: 4,536
  • Joined: 09-June 09

Re: why is this "not assignable? "

Posted 14 August 2012 - 08:59 PM

Quote

I get this error: 'no viable overloaded +=


Your probably getting the error because you have not overloaded the << operator. Remember that the += operator returns a complex object, not something that the output stream can display without operator overload.

Edit: ninja'd

This post has been edited by jjl: 14 August 2012 - 08:59 PM

Was This Post Helpful? 0
  • +
  • -

#9 healix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 29-May 11

Re: why is this "not assignable? "

Posted 14 August 2012 - 09:01 PM

the << is overloaded and tested and works.
Was This Post Helpful? 0
  • +
  • -

#10 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1076
  • View blog
  • Posts: 4,536
  • Joined: 09-June 09

Re: why is this "not assignable? "

Posted 14 August 2012 - 09:06 PM

It's impossible to help without seeing the rest of the code
Was This Post Helpful? 0
  • +
  • -

#11 healix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 29-May 11

Re: why is this "not assignable? "

Posted 14 August 2012 - 09:11 PM

View Postjjl, on 14 August 2012 - 09:06 PM, said:

It's impossible to help without seeing the rest of the code


complex.h

#ifndef complexclass
#define complexclass

#include <iostream>
#include <cmath>
#include <stdexcept>

using namespace std;

class complex
{

    private:
    double a, b;
    
    public:
    complex(double,double);
    complex(double);
    complex();
    double getReal();
    double getImag();
    complex getConjugate();
    float getMod();
    double MagnitudeSq();
    void Print();
    
    bool operator== (complex& param);
    bool operator!= (complex& param);
    
    complex operator+= (complex&);
    complex operator-= (complex& param);
    complex operator*= (complex& param);
    complex operator/= (complex& param);
    
    complex operator- (complex x);
    complex operator+ (complex& param);
    complex operator- (complex& param);
    complex operator* (complex& param);
    complex operator/ (complex& param); 

};
    ostream& operator <<(ostream&,complex &);
    istream& operator>>(istream& in, complex& r);

#endif




.cpp

#include "complex.h"

complex::complex(double aIn,double bIn)
{
    a = aIn;
    b = bIn;
}
complex::complex(double in)
{
    a = in;
    b = 0;

}
complex::complex()
{

    a = 0;
    b = 0;

}
double complex::getReal()
{
    return a;
}
double complex::getImag()
{
    return b;

}
complex complex::getConjugate()
{

        complex temp;
    
        temp.a = a;
        temp.b = -b;
    
        return temp;
    

}
float complex::getMod()
{
    float x;
    
    x=pow(a,2) + pow(b,2);
    x=sqrt(x);
    
    return x;

}
double complex::MagnitudeSq()
{
    return a * a + b * b;

}
complex complex::operator+ (complex& param) 
{
  
    complex temp;
    
    temp.a = a + param.a;
    temp.b = b + param.b;
    
    return temp;
    
}
complex complex::operator-(complex& param) 
{
    
    complex temp;
    
    temp.a = a - param.a;    
    temp.b = b - param.b;
    
    return temp;
    
}

complex complex::operator* (complex& param) 
{
	complex temp;
    
	temp.a = a * param.a - b * param.b;
	temp.b = b * param.a + a * param.b;
    
	return temp;
}

complex complex::operator/ (complex& param) 
{
	complex temp;
    
	double mag = param.MagnitudeSq();
    
	temp.a = a*param.a + b*param.b;
	temp.b = b*param.a - a*param.b;
	temp.a = temp.a/mag;
	temp.b = temp.b/mag;
    
	return temp;
}
bool complex::operator== (complex& param)
{
   
    if((a == param.a) && (b == param.B)/>)
    return true;
    
    return false;
    
}

ostream& operator <<(ostream& out,complex& right)
{
    out<<" The real part is "<< right.getReal() << "The imaginary Part is "<< right.getImag() <<endl;
    
    return out;
}
void complex::Print()
{
   
    cout<< a <<" + "<< b <<"i"<< endl;
   
}
istream& operator>>(istream& in, complex& r)
{
    int real, img;
    
    in >> real; //read real
    
    char c;//if theres a comma read next number
    
    in >> c;
    
    if (c == ',')
        in >> img;
    else
    {
        in.unget();
        img = 1;
    }
    r = complex(real, img);
    return in;


}
bool complex::operator!= (complex& param)
{
    if((a == param.a) && (b == param.B)/>)
        return true;
    
    return false;
}
complex complex::operator- (complex x)
{
    complex temp;
    temp.a = -a;
    temp.b = b;
    
    return temp;

}
complex complex::operator+=(complex& param) {
    a = a + param.getReal();
    b = b + param.getImag();
    return *this;
}

complex complex::operator-= (complex& param)
{}
complex complex::operator*= (complex& param)
{}
complex complex::operator/= (complex& param)
{}



dont mind the first -operator overload.

This post has been edited by healix: 14 August 2012 - 09:13 PM

Was This Post Helpful? 0
  • +
  • -

#12 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: why is this "not assignable? "

Posted 15 August 2012 - 08:50 AM

cout << a += b is parsed as (cout << a) += b. It's telling you that there's no overload for += that takes a stream as its left argument.

View Postblackcompe, on 15 August 2012 - 05:56 AM, said:

If you try to apply the extraction operator to a type who hasn't specified it's behavior, then the compiler doesn't know what to do.


If that were the problem, the compiler would be complaining about a missing overload for <<, not +=.
Was This Post Helpful? 2
  • +
  • -

#13 healix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 29-May 11

Re: why is this "not assignable? "

Posted 15 August 2012 - 09:03 AM

View Postsepp2k, on 15 August 2012 - 08:50 AM, said:

cout << a += b is parsed as (cout << a) += b. It's telling you that there's no overload for += that takes a stream as its left argument.

View Postblackcompe, on 15 August 2012 - 05:56 AM, said:

If you try to apply the extraction operator to a type who hasn't specified it's behavior, then the compiler doesn't know what to do.


If that were the problem, the compiler would be complaining about a missing overload for <<, not +=.


there is an overload for += check out line 163



complex complex::operator+=(complex& param) 
{
    a = a + param.getReal();
    b = b + param.getImag();
    return *this;
}



This post has been edited by healix: 15 August 2012 - 09:04 AM

Was This Post Helpful? 0
  • +
  • -

#14 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: why is this "not assignable? "

Posted 15 August 2012 - 09:22 AM

That doesn't take a stream as its left operand.

I think you missed my point about cout << a += b being parsed as (cout << a) += b.
Was This Post Helpful? 0
  • +
  • -

#15 healix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 29-May 11

Re: why is this "not assignable? "

Posted 15 August 2012 - 09:39 AM

View Postsepp2k, on 15 August 2012 - 09:22 AM, said:

That doesn't take a stream as its left operand.

I think you missed my point about cout << a += b being parsed as (cout << a) += b.


Im totally confused. can you show me an example?

This post has been edited by healix: 15 August 2012 - 09:39 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2