passing a const

i need help on how to pass a const int in a non member function?
imaginary header file:
#pragma once
#include <iostream>
using namespace std;

class Imaginary {
private:
int coeff; //If 5, then means 5i
public:
Imaginary() { coeff = 0; }
Imaginary(int new_coeff) { coeff = new_coeff; }
int get_coeff() { return coeff; }
Imaginary operator+(const Imaginary& rhs);
Imaginary operator-(const Imaginary& rhs);
int operator*(const Imaginary& rhs);
Imaginary operator*(const int& rhs);
Imaginary operator=(const int& rhs);
friend const ostream& operator<<(const ostream& lhs, const Imaginary& rhs);
};


complex header file:
#pragma once
#include <iostream>
#include "imaginary.h"
using namespace std;

class Complex {
private:
int real;
Imaginary imagine;
public:
//YOU: Implement all these functions
Complex(); //Default constructor
Complex(int new_real, Imaginary new_imagine); //Two parameter constructor
Complex operator+(const Complex &rhs);
Complex operator-(const Complex &rhs);
Complex operator*(const Complex &rhs);
bool operator==(const Complex &rhs);
Complex operator^(const int &exponent);
friend ostream& operator<<(ostream &lhs,const Complex& rhs);
friend istream& operator>>(istream &lhs,Complex& rhs);
};




#include <iostream>
#include "complex.h"
using namespace std;

//Class definition file for Complex


Complex::Complex() {
real = 0;
imagine = Imaginary(0);
}

Complex::Complex(int new_real, Imaginary new_imagine) {
real = new_real;
imagine = new_imagine;
}

Complex Complex::operator+(const Complex &rhs) {
int rh_real = rhs.real;
int rh_imagine = rhs.imagine.get_coeff();
return Complex(real + rh_real,Imaginary(imagine + rh_imagine));
}
This is the error:

g++ -c complex.cc
complex.cc: In member function ‘Complex Complex::operator+(const Complex&)’:
complex.cc:23:43: error: passing ‘const Imaginary’ as ‘this’ argument of ‘int Imaginary::get_coeff()’ discards qualifiers [-fpermissive]
int rh_imagine = rhs.imagine.get_coeff();
^
complex.cc: In member function ‘Complex Complex::operator-(const Complex&)’:
complex.cc:29:43: error: passing ‘const Imaginary’ as ‘this’ argument of ‘int Imaginary::get_coeff()’ discards qualifiers [-fpermissive]
int rh_imagine = rhs.imagine.get_coeff();
^
complex.cc: In member function ‘Complex Complex::operator*(const Complex&)’:
complex.cc:35:45: error: passing ‘const Imaginary’ as ‘this’ argument of ‘int Imaginary::get_coeff()’ discards qualifiers [-fpermissive]
int outer = real * rhs.imagine.get_coeff();
^
complex.cc:37:59: error: passing ‘const Imaginary’ as ‘this’ argument of ‘int Imaginary::get_coeff()’ discards qualifiers [-fpermissive]
int last = imagine.get_coeff() * rhs.imagine.get_coeff();
^
complex.cc: In member function ‘bool Complex::operator==(const Complex&)’:
complex.cc:43:48: error: passing ‘const Imaginary’ as ‘this’ argument of ‘int Imaginary::get_coeff()’ discards qualifiers [-fpermissive]
bool imagine_equal = (rhs.imagine.get_coeff() == imagine.get_coeff());
^
make: *** [complex.o] Error 1
[~/imaginary_homework] %
Last edited on
Not enough information.
You haven't show us complex.h nor the declaration of Imaginary.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.
Hint: You can edit your previous post, highlight your code and press the <> formatting button.
Topic archived. No new replies allowed.