Dealing with array of complex numbers program - c++

I am trying to create a program that manipulates with arrays of complex numbers, my program has two classes, class that represents complex numbers and class that represents an array.
Here's the code that i created so far:
Complex.h
#pragma once
#include <functional>
class Complex
{
friend class Array;
double re, im;
friend std::ostream& operator<<(std::ostream&, const Array&);
friend std::istream& operator>>(std::istream&, Array&);
public:
double getRe() const;
double getIm() const;
void setRe(double);
void setIm(double);
Complex operator[](int);
Complex operator=(const Complex&);
Complex operator+(const Complex&);
Complex operator-(const Complex&);
private:
Complex apply(const Complex&, const std::function <double(double, double)> &);
};
Array.h
#pragma once
#include "Complex.h"
#include <functional>
class Array
{
Complex *arr;
int n;
friend std::ostream& operator<<(std::ostream&, const Array&);
friend std::istream& operator>>(std::istream&, Array&);
public:
Array();
Array(int);
Array(const Array&);
Array(Array&&);
~Array();
Array& operator=(const Array&);
Array& operator=(Array &&);
Array operator+(const Array&);
// Array operator-(const Array&);
private:
Array& copy(Array&);
Array& move(Array&);
Array compute(const Array &,const std::function<Complex(Complex, Complex)> &f);
};
Complex.cpp
#include <iostream>
#include "Complex.h"
double Complex::getRe() const
{
return re;
}
double Complex::getIm() const
{
return im;
}
void Complex::setRe(double re)
{
this->re = re;
}
void Complex::setIm(double im)
{
this->im = im;
}
Complex Complex::operator[](int i)
{
Complex temp;
temp.re = re;
temp.im = im;
return temp;
}
Complex Complex::operator=(const Complex &c)
{
re = c.re;
im = c.im;
return *this;
}
Complex Complex::operator+(const Complex &c)
{
*this = apply(c, [](double x, double y) { return x + y; });
return *this;
}
Complex Complex::operator-(const Complex &c)
{
*this = apply(c, [](double x, double y) { return x - y; });
return *this;
}
Complex Complex::apply(const Complex &c, const std::function<double(double, double)> &f)
{
re = re + c.re;
im = im + c.im;
return *this;
}
Array.cpp
Array::Array(int n)
{
this->n = n;
arr = new Complex[n];
}
Array::Array(const Array &a)
{
this->n = a.n;
arr = new Complex[n];
arr = a.arr;
}
Array::Array(Array &&a)
{
this->n = a.n;
arr = a.arr;
a.arr = nullptr;
}
Array& Array::operator=(const Array &a)
{
this->n = a.n;
arr = new Complex[n];
arr = a.arr;
return *this;
}
Array& Array::operator=(Array &&a)
{
this->n = n;
this->arr = a.arr;
a.arr = nullptr;
return *this;
}
std::ostream& operator<<(std::ostream& out, const Array &a)
{
for (int i = 0; i < a.n; i++)
out << a.arr[i].re << " +i" << a.arr[i].im;
return out;
}
std::istream& operator>>(std::istream& in, Array &a)
{
for (int i = 0; i < a.n; i++)
in >> a.arr[i].re >> a.arr[i].im;
return in;
}
Array Array::operator+(const Array &a)
{
return compute(a, [](Complex x, Complex y) { return x + y; });
}
/*
Array Array::operator-(const Array &a)
{
return compute(a, [](Complex x, Complex y) { return x - y; });
}
*/
Array Array::compute(const Array &a,const std::function<Complex(Complex , Complex)> &f)
{
for (int i = 0; i < n; i++)
arr[i] = f(arr[i], a.arr[i]);
return *this;
}
Array::~Array()
{
delete[] arr;
n = 0;
}
Source.cpp
#include <iostream>
#include "Array.h"
#include "Complex.h"
int main()
{
Array a(2);
std::cin >> a;
Array b;
b = a;
Array c(2);
c = a + b;
std::cout << c;
getchar();
getchar();
}
So this is what i have at the moment, i need to add that this worked just fine before, when in my Complex class i only had setters, getters, these fields and Array class as a friend, which means that i was doing indexing "manually" and operations + and - without overloading of corresponding operators in Complex class.
However, i had an idea that it would be better (and more in the spirit of oop) to overload the operators that i need for Complex class since code would be more readable.
However, even though i get the result i need, every time after execution of program, when i hit enter, error with destructor appears, i tried some debugging and it appears to me that problem appears when object Array b is "destructed", but i could not figure out what actually happens (and is b actually causing the problem in the first place).
NOTE: I've found some questions here related to complex arrays but non of them solved my problem.
Any help appreciated!

Related

Changing type of template at run time

I'm trying to make a Matrix struct which would work with various data types, including my Complex struct:
struct Complex {
double re = 0, im = 0;
Complex operator*(const Complex& other) const {
return Complex(re * other.re - im * other.im, im * other.re + re * other.im);
}
Complex operator*(const double& other) const {
return Complex(re * other, im * other);
}
Complex() {}
Complex(double a) : re(a) {}
Complex(double a, double b) : re(a), im(b) {}
};
std::ostream& operator<<(std::ostream& out, Complex z) {
out << z.re << " " << z.im << "i";
return out;
}
template <typename T>
Complex operator*(const T& c, const Complex& z) {
return z * c;
}
The obvious way is to make a template like one in the code below:
template <typename T>
struct Matrix {
std::vector<T> m;
unsigned int rows, cols;
Matrix<Complex> operator*(const Complex& z) const {
Matrix<Complex> result(rows, cols);
for (int i = 0; i < m.size(); i++) {
result.m[i] = m[i] * z;
}
return result;
}
void operator*=(const Complex& z) {
(*this) = (*this) * z; // <- ideally we're trying to get this to work
}
void operator=(const Matrix<T>& other) {
rows = other.rows;
cols = other.cols;
m.resize(rows * cols);
m = other.m;
}
Matrix(const unsigned int& rows, const unsigned int& cols) : rows(rows), cols(cols) {
m.resize(rows * cols);
}
Matrix(const Matrix<T>& other) : rows(other.rows), cols(other.cols) {
(*this) = other;
}
};
int main() {
Complex z(1, 1);
Matrix<double> A(1, 1);
A.m[0] = 2.0;
std::cout << (A * z).m[0] << std::endl; // works as it should because a temporary Matrix<Complex> gets created
A *= z; // and here we're introducing the problem
std::cout << A.m[0] << std::endl;
}
The problem arises when calling *= operator. We're trying to call an unexisting = operator overload. My first attempt was to write something like this instead:
template <typename T_other>
void operator=(const Matrix<T_other>& other) {
rows = other.rows;
cols = other.cols;
m.resize(rows * cols);
for (int i = 0; i < m.size(); i++) {
m[i] = other.m[i];
}
}
This however leads to other problems:
The type of A is still Matrix<double> and after the multiplication it should be Matrix<Complex> to store complex numbers.
There is no conversion from Complex to double as it results in loss of data (the imaginary part).
Also, I would like to avoid creating a template specialization for Matrix<Complex>, but if there's no other way I'll accept it.
C++ is statically typed. Once you declare a variable and type, you can't change the type of that variable.
template <typename T>
struct Matrix {
void operator*=(const Complex& z) {
(*this) = (*this) * z;
}
}
The *= operator overload for your Matrix doesn't make sense. A Complex can hold the value of a double with imaginary part 0, but a double can never hold the value of a Complex.
Multiplying a real matrix by a complex number necessarily produces a complex matrix. So you try and assign the complex RHS to the real LHS - and either that makes no sense and shouldn't be done, or you have some idea for how to convert that (e.g. keep real part, keep absolute value etc) and then have to implement a Matrix<double> constructor from Matrix<Complex>.
Real numbers are a subset of complex numbers, so just make A a Matrix<Complex> from the beginning if you ever want to make it complex later.

How do I return results while using operator overloading +

I have the code that confused me when I'm using operator overloading and returning the temp object it calls my copy constructor and I'm getting exception but when I'm returning my class member temp.size it calls my parameterized constructor MyClass(int size) and everything works fine. I'm interested in how it works and what it's related to. Code below.
class MyClass
{
private:
int* data;
int size;
public:
MyClass()
{
size = 0;
}
MyClass(int size)
{
this->size = size;
data = new int[size];
for (size_t i = 0; i < size; i++)
{
data[i] = rand();
}
}
MyClass(const MyClass& obj)
{
this->size = obj.size;
this->data = new int[size];
for (size_t i = 0; i < size; i++)
{
data[i] = obj.data[i];
}
}
MyClass operator+(const MyClass& obj)
{
MyClass temp;
temp.size = this->size + obj.size;
return temp.size;
}
friend ostream& operator<<(ostream& os, MyClass& obj);
~MyClass()
{
delete[]data;
}
};
ostream& operator<<(ostream & os, MyClass & obj)
{
os << obj.size;
return os;
}
int main()
{
MyClass a(5);
MyClass b(a);
MyClass c = a + b;
cout << c;
return 0;
}
I see several issues with your code:
your default constructor is not initializing data.
your operator+ is not attempting to copy values from this->data and obj.data into temp.data, thus leaving temp.data uninitialized.
your operator+ is returning the wrong MyClass object. It goes to the trouble of preparing a MyClass object named temp, and then completely discards temp upon exit. By passing temp.size to return, you are creating another MyClass object via your MyClass(int size) constructor, which generates all new random data. You need to instead return the temp object that you prepared. Then the compiler will either call your copy constructor to assign temp to c in main(), or it will optimize away the copy to let operator+ operate directly on c.
you are missing a copy assignment operator, per the Rule of 3. Nothing in your example actually invokes a copy assignment, but you need to implement the operator properly nonetheless. And if you are using C++11 or later, you should also follow the Rule of 5 as well, by adding a move constructor and a move assignment operator. However, if you can change your design to use std::vector instead of new[], then you can follow the Rule of 0 and let the compiler do all the hard work for you.
With that said, try something more like this:
#include <iostream>
#include <algorithm>
#include <utility> // C++11 and later only...
#include <cstdlib>
#include <ctime>
class MyClass
{
private:
int* data;
size_t size;
public:
MyClass(size_t size = 0) : data(NULL), size(size)
{
if (size > 0)
{
data = new int[size];
// in C++11 and later, consider using std::uniform_int_distribution instead of rand()!
std::generate(data, data + size, std::rand);
}
}
MyClass(const MyClass& obj) : data(NULL), size(obj.size)
{
if (size > 0)
{
data = new int[size];
std::copy(obj.data, obj.data + obj.size, data);
}
}
// C++11 and later only...
MyClass(MyClass&& obj) : data(NULL), size(0)
{
std::swap(size, obj.size);
std::swap(data, obj.data);
}
~MyClass()
{
delete[] data;
}
MyClass& operator=(const MyClass& rhs)
{
if (&rhs != this)
{
MyClass temp(rhs);
std::swap(size, temp.size);
std::swap(data, temp.data);
}
return *this;
}
MyClass& operator=(MyClass&& rhs)
{
MyClass temp(std::move(rhs));
std::swap(size, temp.size);
std::swap(data, temp.data);
return *this;
}
MyClass operator+(const MyClass& obj) const
{
MyClass temp;
temp.size = size + obj.size;
if (temp.size > 0)
{
temp.data = new int[temp.size];
std::copy(data, data + size, temp.data);
std::copy(obj.data, obj.data + obj.size, temp.data + size);
}
return temp;
}
friend std::ostream& operator<<(std::ostream& os, const MyClass& obj);
};
std::ostream& operator<<(std::ostream& os, const MyClass& obj)
{
os << obj.size;
for(size_t i = 0; i < obj.size; ++i)
{
os << " " << obj.data[i];
}
return os;
}
int main()
{
std::srand(std::time(0));
MyClass a(5);
MyClass b(a);
MyClass c = a + b;
std::cout << c;
return 0;
}
Which can be simplified to this:
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
class MyClass
{
private:
std::vector<int> data;
public:
MyClass(size_t size = 0) : data(size)
{
std::generate(data.begin(), data.end(), std::rand);
}
MyClass operator+(const MyClass& obj) const
{
MyClass temp;
if (!data.empty() || !obj.data.empty())
{
temp.data.reserve(data.size() + obj.data.size());
temp.data.insert(temp.data.end(), data.begin(), data.end());
temp.data.insert(temp.data.end(), obj.data.begin(), obj.data.end());
}
return temp;
}
friend std::ostream& operator<<(std::ostream& os, const MyClass& obj);
};
std::ostream& operator<<(std::ostream& os, const MyClass& obj)
{
os << obj.size;
for(size_t i = 0; i < obj.size; ++i)
{
os << " " << obj.data[i];
}
return os;
}
int main()
{
std::srand(std::time(0));
MyClass a(5);
MyClass b(a);
MyClass c = a + b;
std::cout << c;
return 0;
}
If you can use std::vector, your code can become:
class MyClass {
private:
vector<int> data;
public:
MyClass(int size) {
data.resize(size);
for (size_t i = 0; i < size; i++) {
data[i] = rand();
}
}
MyClass operator+(const MyClass& obj) {
return data.size() + obj.data.size();
}
friend ostream& operator<<(ostream& os, MyClass& obj);
};
ostream& operator<<(ostream& os, MyClass& obj) {
os << obj.data.size();
return os;
}
Otherwise do not delete on an uninitialized pointer. (so either call new or set to nullptr).
You do not initialize the pointer in the default constructor, so do not call delete in the destructor.
Change the default constructor to set data to nullptr
MyClass() {
data = nullptr;
size = 0;
}
Also do not copy from an array when you have not initialized the pointer. When you set the size variable you are not creating the array. You should create a function called setSize and change your code to this:
MyClass(int size) {
data = nullptr;
setSize(size);
}
void setSize(int size) {
this->size = size;
delete[] data;
data = new int[size];
for (size_t i = 0; i < size; i++) {
data[i] = rand();
}
}
MyClass operator+(const MyClass& obj) {
MyClass temp;
temp.setSize(this->size + obj.size);
return temp.size;
}
Whenever you need to change the size, you should use the setSize function to make sure the array is created as well.

C++ My first class operator overload, with header

im new to C++, although i do know the general C syntax. I've been trying to create a class with operator overloading. But i can't get it to work. Well partially got it to work.
Working operator overloading in same source:
//test.cpp
#include <iostream>
class Fraction
{
int gcd(int a, int b) {return b==0 ? a : gcd(b,a%b); }
int n, d;
public:
Fraction(int n, int d = 1) : n(n/gcd(n,d)), d(d/gcd(n,d)) {}
int num() const { return n; }
int den() const { return d; }
Fraction& operator*=(const Fraction& rhs) {
int new_n = n*rhs.n / gcd(n*rhs.n, d*rhs.d);
d = d*rhs.d / gcd(n*rhs.n, d*rhs.d);
n = new_n;
return *this;
}
};
std::ostream& operator<<(std::ostream& out, const Fraction& f){
return out << f.num() << '/' << f.den() ;
}
bool operator==(const Fraction& lhs, const Fraction& rhs) {
return lhs.num() == rhs.num() && lhs.den() == rhs.den();
}
bool operator!=(const Fraction& lhs, const Fraction& rhs) {
return !(lhs == rhs);
}
Fraction operator*(Fraction lhs, const Fraction& rhs)
{
return lhs *= rhs;
}
int main()
{
Fraction f1(3,8), f2(1,2), f3(10,2);
std::cout << f1 << '*' << f2 << '=' << f1*f2 << '\n'
<< f2 << '*' << f3 << '=' << f2*f3 << '\n'
<< 2 << '*' << f1 << '=' << 2 *f1 << '\n';
}
Output:
3/8*1/2=3/16
1/2*5/1=5/2
2*3/8=3/4
Source: http://en.cppreference.com/w/cpp/language/operators
Now my code, trying to apply the code from above
//vectors.h
class Vector2
{
public:
Vector2(void);
~Vector2(void);
int counter;
Vector2& operator+=(const Vector2& vec);
}
//vectors.cpp
#include "vectors.h"
Vector2::Vector2(void)
{
counter = 0;
}
Vector2::~Vector2(void)
{
}
Vector2& operator+=(Vector2& vec)//error: too few parameters
{
int new_n = counter + vec.counter;
counter = new_n;
return *this;//error: this may only be used in a non-static member function.
}
//main.cpp
#include <stdio.h>
#include "vectors.h"
int main(void)
{
Vector2 vector();
while(true)
{
vector += vector;//error: expression must be a modifiable value
printf("Vector counter: %d\n",vector.counter);
}
}
What i'm trying to do:
I'm trying to make my own class, and use operator overloading. But the part i can't get to work is defining the class with a header while keeping operator overloading working.
Thanks for reading my question
The following compiled in ideone: http://ideone.com/ratVVT
Changes are:
Implementation of (overload) method must specify the Class name
Implementation of (overload) method must have the same signature as the declaration (missing const).
Declaring the variable vector in main Vector2 vector(); was interpreted as a function declaration, instead of a Vector2 variable.... use Vector2 vector; or Vector2 vector=Vector2()
The code copied below.
#include <iostream>
//vectors.h
class Vector2
{
public:
Vector2();
~Vector2();
int counter;
Vector2& operator+=(const Vector2& vec);
};
//vectors.cpp
//#include "vectors.h"
Vector2::Vector2()
{
counter = 0;
}
Vector2::~Vector2()
{
}
Vector2& Vector2::operator+=(const Vector2& vec)// <---- CHANGE
{
int new_n = counter + vec.counter;
counter = new_n;
return *this;//error: this may only be used in a non-static member function.
}
//main.cpp
#include <stdio.h>
//#include "vectors.h"
int main()
{
Vector2 vector; // <---- CHANGE
while(true)
{
vector += vector;
printf("Vector counter: %d\n",vector.counter);
}
}
You are missing the class name in your method definition Vector2:: and the signature does not match because of the first parameter missing a const.
Vector2& Vector2::operator+=(const Vector2& vec)

Strange "Member function not viable" error in templated linear algebra vector class

I'm implementing a templated vector class (not the data container, but the vector in the linear algebra sense), and I'm getting quite a few errors whenever I refer to rhs in my operator overloading. Also, my copy constructor doesn't seem to be working.
#ifndef __VecXd__VecXd__
#define __VecXd__VecXd__
#define ULL unsigned long long
#include <iostream>
using namespace std;
template <class T>
class VecXd
{
public:
explicit VecXd(ULL newDimension = 1) { dimension = newDimension; vector = new T[newDimension];}
VecXd(const VecXd<T> &rhs);
VecXd<T>& operator=(const VecXd<T> &rhs);
const VecXd<T> operator+(const VecXd<T> &rhs) const;
VecXd<T>& operator+=(const VecXd<T> &rhs);
friend ostream& operator<<(ostream &out, VecXd<T> vec);
friend istream& operator>>(istream &in, VecXd<T>& vec);
~VecXd() { delete[] vector; }
const ULL getDimension() { return dimension; }
const T itemAtIndex(ULL index) { if(index >= dimension) throw 1; return vector[index]; }
private:
ULL dimension;
T *vector;
};
template <class T>
VecXd<T>::VecXd(const VecXd<T> &rhs)
{
dimension = rhs.getDimension();
vector = new T[dimension];
for (ULL i = 0; i < dimension; ++i)
vector[i] = rhs.itemAtIndex(i);
}
template <class T>
VecXd<T>& VecXd<T>::operator=(const VecXd<T> &rhs)
{
if (this != &rhs)
{
if (dimension != rhs.getDimension())
{
delete [] vector;
dimension = rhs.getDimension();
vector = new T[dimension];
}
for (ULL i = 0; i < dimension; ++i)
vector[i] = rhs.itemAtIndex(i);
}
return *this;
}
template <class T>
VecXd<T>& VecXd<T>::operator+=(const VecXd<T> &rhs)
{
if (dimension != rhs.getDimension())
{
cout << "\nCannot perform addition. Vectors do not have the same dimensions.\n";
throw 1;
}
else
{
for (ULL i = 0; i < dimension; ++i)
vector[i] += rhs[i];
}
return *this;
}
template <class T>
const VecXd<T> VecXd<T>::operator+(const VecXd<T> &rhs) const
{
VecXd<T> temp = *this;
temp += rhs;
return temp;
}
template <class T>
ostream& operator<<(ostream &outs, VecXd<T> vec)
{
for (ULL i = 0; i < vec.dimension; ++i)
out << vec.vector[i] << (i+1 < vec.dimension ? " " : "");
return out;
}
template <class T>
istream& operator>>(istream &in, VecXd<T> &vec)
{
ULL newDim = 1;
cin >> newDim;
if (!cin.good())
{
cout << "\nImproper input.\n";
throw 1;
}
else
{
delete [] vec.vector;
vec.dimension = newDim;
vec.vector = new T[vec.dimension];
for (ULL i = 0; i < vec.dimension; ++i)
in >> vec.vector[i];
}
return in;
}
#endif /* defined(__VecXd__VecXd__) */
I'm getting errors of this style:
Member function 'getDimension' not viable: 'this' argument has type 'const VecXd<int>', but function is not marked const
This happens every time rhs calls a function (such as getDimension() or itemAtIndex()); two error appear (once for VecXd<int> and another for VecXd<int>).
Also, the copy constructor is not recognized in the overloaded +operator function in this line:
VecXd<T> temp = *this;
Help?
To be able to call a function on a const object, you need to promise the compiler that the function will not modify the object. To do that, you mark the function with the keyword const after its argument list. For example, to make getDimension a const member function, you would change it to:
const ULL getDimension() const { return dimension; }
(Note that the const in the return type will have absolutely no effect, so you should get rid of it)

How to Overload operator+ with Polynomial Class and What Types to Return

I'm having trouble overloading the + operator, and I can't figure out what the cause is. The + operator returns a Polynomial (called C) but it returns it by value, where as the assignment operator is expecting a Polynomial object as a parameter passed by reference. To make C=A+B work, do I need to have a second assignment operator function that takes a Polynomial passed by value as an argument? Thanks!
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
void line(int lines);
class Polynomial
{
private:
int degree;
int* coeffs;
public:
//constructors
Polynomial() {degree=0;coeffs=new int[1];}
Polynomial(int deg) {degree=deg;coeffs=new int[deg+1];}
Polynomial(const Polynomial& A);
//mutators
void GetCoeffs(istream& in);
void EditCoeff(int deg);
void ResetCoeffs();
int Coeff(int deg);
void Randomize(int max);
//accessors
void Show(ostream& out);
int Degree() {return degree;}
//operators
friend Polynomial operator+(Polynomial& A, Polynomial& B);
void operator =(Polynomial A);
};
int main()
{
Polynomial A(5);
Polynomial B(5);
A.Randomize(5);
B.Randomize(5);
A.Show(cout);
line(2);
B.Show(cout);
line(2);
Polynomial C(5);
C=A+B;
C.Show(cout);
return 0;
}
void Polynomial::Randomize(int max)
{
for (int i=degree; i>=0; i--)
{
coeffs[i]=rand()%(max+1) + 1;
if ((rand()%(101) + 1)%2 == 0)
coeffs[i]*=-1;
}
}
void Polynomial::operator =(Polynomial A)
{
if (degree==A.degree)
{
for (int i=degree; i>=0; i--)
{
coeffs[i]=A.coeffs[i];
}
}
}
Polynomial Polynomial::operator+(Polynomial& A, Polynomial& B)
{
Polynomial C;
if (A.degree>=B.degree)
{
C=A;
for (int i=B.degree; i>=0; i--)
{
C.coeffs[i]=A.coeffs[i]+B.coeffs[i];
}
C.Show(cout);
return C;
}
else
{
C=B;
for (int i=A.degree; i>=0; i--)
{
C.coeffs[i]=A.coeffs[i]+B.coeffs[i];
}
C.Show(cout);
return C;
}
}
int Polynomial::Coeff(int deg)
{
return coeffs[deg];
}
void line(int lines)
{
for (int i=0; i<lines; i++)
cout << endl;
}
void Polynomial::GetCoeffs(istream& in)
{
for (int i=degree; i>=0; i--)
{
in >> coeffs[i];
}
in.ignore();
}
void Polynomial::Show(ostream& out)
{
for (int i=degree; i>=0; i--)
{
if (coeffs[i]>=0)
{
if (i!=degree)
out << " + ";
out << coeffs[i];
}
else
{
if (coeffs[i]<0)
out << " - ";
out << 0-coeffs[i];
}
if (i>1)
out << "x^" << i;
else if (i==1)
out << "x";
}
}
Polynomial::Polynomial(const Polynomial& A)
{
coeffs=new int[A.degree+1];
degree=A.degree;
for (int i=A.degree; i>=0; i--)
{
coeffs[i]=A.coeffs[i];
}
}
This issue here is you have mixed up the global(Outside class definitions) operator+ and the member class definition of operator+.
(static) Polynomial operator+(Polynomial& A, Polynomial& B); This operator is used globally, so outside of a class.
Inside the class you need to use the following signature.
Polynomial& operator+(const Polynomial& other);
Here is an example.
Polynomial p;
Polynomial q;
p = p + q;
The code for this if the operator is define in the class is:
p = p.operator+(q); //only needs one parameter.
The code for this if the operator is define globally is:
p = ::operator+(p, q); //needs both parameter
NOTE:
To use it as a non member function remove Polynomial operator+(Polynomial& A, Polynomial& B); from your definition Polynomial Polynomial::operator+(Polynomial& A, Polynomial& B){/**/} should be move above the main function and it now becomes:
static Polynomial operator+(Polynomial& A, Polynomial& B){/**/}
Normally you would overload operator+ in one of two ways:
Polynomial operator+(const Polynomial &other) const;
or as a non-member function,
Polynomial operator+(Polynomial a, const Polynomial &b);
You don't normally need the friend qualifier for the latter since the implementation will likely be in terms of another overloaded operator already defined in the class:
Polynomial operator+=(const Polynomial &other);
Then your non-member implementation will just be:
Polynomial operator+(Polynomial a, const Polynomial &b)
{
a+=b;
return a;
}
If you insist on using the function as defined in your code, then you will need to make it a
friend if you need access to private members:
Polynomial operator+(Polynomial &a, Polynomial &b)
{
Polynomial p;
// add them as needed
return p;
}