I am a beginner in C++. I am learning on how to overload operators. I have created a class Complex that represents complex numbers and methods for complex arithmetic and a class ComplexArray that represents fixed-length arrays of elements in complex vector space C.
I get compiler errors, that it is unable to find the correct form of operator[]. However, I searched the internet and I am unable to rectify the error. Any hints/tips in the right direction would be of tremendous help.
Severity Code Description Project File Line Suppression State
Error C2676 binary '[': 'const ComplexArray' does not define this operator or a conversion to a type acceptable to the predefined operator ComplexArrays c:\users\quasa\source\repos\complexarrays\complexarrays\testcomplexarray.cpp 7
Here is my code:
TestComplexArray.cpp
#include <iostream>
#include "ComplexArray.h"
Complex ComplexSum(const ComplexArray& cArray, int size)
{
Complex sum = cArray[0];
for (int i = 1; i < size; i++)
{
sum = sum + cArray[i];
}
return sum;
}
Complex ComplexProduct(const ComplexArray& cArray, int size)
{
Complex product = cArray[0];
for (int j = 1; j < size; j++)
{
product = product * cArray[j];
}
return product;
}
int main()
{
char ch;
const int size = 5;
ComplexArray cArray(size);
for (int i = 0; i < size; i++)
{
cArray[i] = Complex((double)(i + 1), 0);
std::cout << cArray[i];
}
Complex sum = ComplexSum(cArray, size);
Complex product = ComplexProduct(cArray, size);
std::cout << "Sum = " << sum << std::endl;
std::cout << "Product = " << product << std::endl;
std::cin >> ch;
return 0;
}
ComplexArray.h
class ComplexArray
{
private:
Complex* complexArr;
int size;
ComplexArray();
public:
//Constructors and destructors
ComplexArray(int size);
ComplexArray(const ComplexArray& source);
virtual ~ComplexArray();
//Range for the complexArr
int MaxIndex() const;
//Overload the indexing operator
const Complex& operator [](int index) const;
Complex& operator [](int index);
};
ComplexArray.cpp
#include "Complex.h"
#include "ComplexArray.h"
ComplexArray::ComplexArray(int s)
{
size = s;
complexArr = new Complex[size];
}
ComplexArray::ComplexArray(const ComplexArray& source)
{
//Deep copy source
size = source.size;
complexArr = new Complex[size];
for (int i = 0; i < size; i++)
{
complexArr[i] = source.complexArr[i];
}
}
ComplexArray::~ComplexArray()
{
delete[] complexArr;
}
int ComplexArray::MaxIndex() const
{
return (size - 1);
}
/*
c1.operator[](int index) should return a reference to the Complex
object, because there are two possible cases.
Case 1:
Complex c = complexArray[3];
Case 2:
complexArray[3] = c;
In the second case, complexArray[3] is an lvalue, so it must return
a Complex object by reference, so that it can be assigned to.
*/
const Complex& ComplexArray::operator[] (int index) const
{
return complexArr[index];
}
Complex& ComplexArray::operator[](int index)
{
return complexArr[index];
}
Complex.h
#include <iostream>
class Complex
{
private:
double x;
double y;
void init(double xs, double ys); //Private helper function
public:
//Constructors and destructors
Complex();
Complex(const Complex& z);
Complex(double xs, double ys);
virtual ~Complex();
//Selectors
double X() const;
double Y() const;
//Modifiers
void X(double xs);
void Y(double ys);
//Overload binary +, = and * operators
Complex operator + (const Complex& z);
Complex& operator = (const Complex& z);
Complex operator * (const Complex& z) const;
//Overload unary - operator
Complex operator -() const;
friend Complex operator * (const double alpha, const Complex& z);
friend Complex operator * (const Complex& z, const double beta);
//Overload << operator
friend std::ostream& operator << (std::ostream& os, const Complex& z);
//A complex function f(z)=z^2
Complex square();
};
As you have all pointed out - I was missing the forward definition of a #include.
Complex.cpp has the header
#include "Complex.h"
ComplexArray.h has the header
#include "Complex.h"
ComplexArray.cpp has the header
#include "ComplexArray.h"
TestComplexNumbers.cpp has the header
#include <iostream>
#include "ComplexArray.h"
My compile-time errors have been resolved.
I don't think the error comes from operator[], as you can see in the function:
Complex ComplexSum(const ComplexArray& cArray, int size)
{
Complex sum = cArray[0];
for (int i = 1; i < cArray.MaxIndex(); i++)
{
sum = sum + cArray[i];
}
}
You don't return a result. That's fatal.
ComplexArray depends on Complex but order of includes doesn't look right
#include "ComplexArray.h"
#include "Complex.h"
You have to forward-declare Complex before ComplexArray
class Complex;
Code fails at \testcomplexarray.cpp line 7 which is
Complex sum = cArray[0];
It looks like you have problem with ctors of Complex. Be sure that you have NOT defined such:
Complex(Complex& v); // that's bad. it prevents to use copy constructor
If you need copy ctor for some inconceivable reason, it always should look so:
Complex(const Complex& v);
Related
I was trying to make a Complex class which would represent the Complex numbers in c++ but I have encountered an error. The copy constructor seems to be the problem but I am not sure as to what is wrong or why it is wrong. The / operator works well as I return a reference to an object of Complex class. but the * operator does not works when I return an object of Complex class. Please explain this behaviour,
edit: I have used the new operator and a custom copy constructor as it was a requirement of my school to dynamically allocate memory.
here is my code.
#include <iostream>
using namespace std;
class Complex
{
public:
double r, i;
Complex() : r(0), i(0) {}
Complex(int r, int i)
{
this->r = double(r);
this->i = double(i);
}
Complex(double r, int i)
{
this->r = r;
this->i = double(i);
}
Complex(int r, double i)
{
this->r = double(r);
this->i = i;
}
Complex(double r, double i)
{
this->r = r;
this->i = i;
}
Complex(Complex &c)
{
cout<<"cpy constructor called"<<endl;
r = c.r;
i = c.i;
}
void operator()(float r, float i)
{
this->r = r;
this->i = i;
}
void operator()(int r, int i)
{
this->r = r;
this->i = i;
}
Complex operator+(Complex &c)
{
Complex *c1 = new Complex(r + c.r, i + c.i);
return *c1;
}
Complex operator-(Complex &c)
{
Complex *c1 = new Complex(r - c.r, i - c.i);
return *c1;
}
Complex operator*(Complex &c)
{
Complex *c1 = new Complex(
r * c.r + (-1) * (i * c.i),
r * c.i + i * c.r);
return *c1;
}
Complex& operator/(Complex &c)
{
float dem;
Complex num;
num = (*this) * * (new Complex(c.r,-c.i));
dem = c.r*c.r + (c.i*c.i);
Complex *c1 = new Complex(num.r/dem,num.i/dem);
return *c1;
}
operator string()
{
string temp, s;
temp = to_string(r);
while (true)
{
if (temp[temp.length() - 1] == '0')
{
temp.pop_back();
}
else
break;
}
if (temp[temp.length() - 1] == '.')
temp.pop_back();
s += temp;
if (i >= 0)
{
s += '+';
}
temp = to_string(i);
while (true)
{
if (temp[temp.length() - 1] == '0')
{
temp.pop_back();
}
else
break;
}
if (temp[temp.length() - 1] == '.')
temp.pop_back();
s += temp;
s += 'i';
return s;
}
friend ostream &operator<<(ostream &out, Complex c);
};
ostream& operator<<(ostream &out, Complex c)
{
out << string(c);
return out;
}
int main()
{
Complex c(5, 5);
Complex d(5,5);
cout << c/d <<endl;
cout<< c*d <<endl;
}
and this is the error I encounter after this
test.cpp: In function 'int main()':
test.cpp:123:13: error: invalid initialization of non-const reference of type 'Complex&' from an rvalue of type 'Complex'
cout<< c*d <<endl;
~^~
test.cpp:31:5: note: initializing argument 1 of 'Complex::Complex(Complex&)'
Complex(Complex &c)
^~~~~~~
test.cpp:111:10: note: initializing argument 2 of 'std::ostream& operator<<(std::ostream&, Complex)'
ostream& operator<<(ostream &out, Complex c)
This is the image to the error
Repeat the pattern of the code below for your other operators. Note the operator takes the parameter by const ref and returns the result by value:
Complex operator+(Complex const &c)
{
return Complex(r + c.r, i + c.i);
}
The copy constructor should also be changed to take it's parameter by const ref ie
Complex(Complex const &c)
{
r = c.r;
i = c.i;
}
The minimal fix isComplex(Complex &c) -> Complex(const Complex &c)
But then there will remain leaks due to the inappropriate usage of new there, after some cleanup:
#include <iostream>
using namespace std;
class Complex
{
public:
double r, i;
Complex(double r, double i) : r(r), i(i) {}
Complex operator+(Complex c)
{
return Complex(r + c.r, i + c.i);
}
Complex operator-(Complex c)
{
return Complex(r - c.r, i - c.i);
}
Complex operator*(Complex c)
{
return Complex(
r * c.r + (-1) * (i * c.i),
r * c.i + i * c.r);
}
Complex operator/(Complex c)
{
auto num = Complex (*this) * Complex(c.r,-c.i);
auto dem = c.r*c.r + (c.i*c.i);
return Complex(num.r/dem,num.i/dem);
}
operator string()
{
string temp, s;
temp = to_string(r);
while (!temp.empty() && temp.back() == '0')
temp.pop_back();
if (!temp.empty() && temp.back() == '.')
temp.pop_back();
s += temp;
if (i >= 0)
{
s += '+';
}
temp = to_string(i);
while (!temp.empty() && temp.back() == '0')
temp.pop_back();
if (!temp.empty() && temp.back() == '.')
temp.pop_back();
s += temp;
s += 'i';
return s;
}
friend ostream &operator<<(ostream &out, Complex c);
};
ostream& operator<<(ostream &out, Complex c)
{
out << string(c);
return out;
}
int main()
{
Complex c(5, 5);
Complex d(5,5);
cout << c/d <<endl;
cout<< c*d <<endl;
}
The big problem is lack of const on your references and members. The second problem is not using =default. The third problem is too many overloads. Forth, your new use is wrong. operstor() are all wrong. The last problem is your operator strategy. And don't use namespace std;.
struct Complex
Complex is both default public and basically an aggregate. I'd use struct
{
double r=0
double i=0;
you can do default initialization in C++ now.
Complex()=default;
now this ctor is auto-written, because we initialized the members at declaration. DRY is "don't repeat yourself"; when easy, don't repeat member names.
Complex(double r_in, double i_in):r(r_in),i(i_in){}
ctors should construct, not assign, members.
Drop the other 2 arg ctors. Callers can convert.
Complex(Complex const&c)=default;
const here, and let the compiler write a member-wise copy with =default; DRY.
Complex(Complex &&c)=default;
Complex& operator=(Complex const&c)=default;
Complex& operator=(Complex &&c)=default;
also assignment and moves
Now other operators...
Complex& operator+=(Complex const& o)&{
implement a+=b before +.
r+=o.r;
i+=o.i;
return *this;
}
here we repeat member names; it isn't easy to avoid.
Do the same for -= and *=
Next,
friend Complex operator+(Complex lhs, Complex const& rhs){
lhs+=rhs;
return lhs;
}
this makes a non-member +. It makes a copy of its left hand argument by taking it by value, and a const reference to its right hand argument.
We then increment the left hand copy by the right, and return it.
This pattern chans wonderfully in 99% of situations;
Complex x=a+b+c
becomes
Complex x=a;
x+=b;
x+=c;
after accounting for moves and elision.
And you get a+=b; and a+b by writing only one member-wise operation.
Just don't use your operator(), they are nonsense.
So I created a class called myClass that takes in an int and has a private variable that stores the int as a vector in binary (i.e. 12 is '1100'). I want to define an operator that adds two myClass variables together as a vector of bools (aka bit-wise operation).
Here is the code I have:
class myClass {
public:
myClass();
myClass(int a);
myClass& operator+(const myClass& value);
private:
std::vector<bool> bit;
};
I want this to work in the main function:
int main() {
std::cin >> value;
Integer a = value;
std::cin >> value;
Integer b = value;
myClass c = a+b;
return 0;
}
Operator definition:
myClass myClass::operator+(const myClass& rhs) {
Integer c = // not sure what to do here
return c;
}
The part that's confusing me is that it must take in an integer but then the operator does the operation on the vector of bools.
Well obviously you need to do the same as when you add normal numbers on paper. Start with the lowest significance bits, and add them together. If the result overflows (eg. binary 1+1=10) then remember that overflow for the next iteration.
I'd strongly suggest that you first create constructor that takes bool array for your class:
myClass(std::vector<bool> bits);
We'll use that in the implementation. Now what you want is to add the lists of bools. I have created an implementation that doesn't care how big the lists are. This will be handy if you want to calculate with huge integers:
#include <vector>
bool add_bools(const bool A, const bool B) {
return !(A && B) && (A || B);
}
/** Loops over vectors and adds the booleans in them
the booleans are considered to be in little endian order
so that the least significant is first in the array. **/
std::vector<bool> add_vectors(const std::vector<bool>& first,
const std::vector<bool>& second) {
std::vector<bool> result;
// Remembers that previous addition produced false when
// we add true+true
bool overflow = false;
const int bits = first.size()>second.size()?first.size():second.size();
for (int i = 0; i < bits || overflow; ++i) {
bool bitA, bitB;
bitA = i<first.size() ? first[i]:false;
bitB = i<second.size() ? second[i]:false;
bool tmp_result = add_bools(bitA, bitB);
// remember to add overflow from previous iteration
result.push_back(add_bools(tmp_result, overflow));
// remember overflow for next iteration
overflow = (bitA&&bitB) || (overflow && tmp_result);
}
return result;
}
#include <iostream>
void test_add_vectors() {
std::vector<bool> first;
std::vector<bool> second;
const int bits = 5;
for (int i = 0, l = bits; i < l; ++i) {
first.push_back(false);
second.push_back(true);
}
first[0] = true;
std::vector<bool> result = add_vectors(first, second);
for (int i = 0, l = result.size(); i < l; ++i) {
std::cout<< (result[i]?'1':'0')<<" ";
}
}
You can use that implementation like this, making use of the constructor that takes bool array:
myClass myClass::operator+(const myClass& rhs) {
myClass result(add_vectors(bit, rhs.bit));
return result;
}
You need to define a way to go to and from an integer representation. Here's a rough idea:
#include <vector>
#include <iostream>
class myClass {
private:
void setInt(int x) {
bit.clear();
while (x) {
if (x & 1)
bit.push_back(1);
else
bit.push_back(0);
x>>=1;
}
reverse(bit.begin(), bit.end());
}
public:
int toInt() const {
int i = 0;
for (size_t b = 0; b < bit.size(); b++) {
if (bit[bit.size() - 1 - b])
i |= 1<<b;
}
return i;
}
myClass(int a) {
setInt(a);
}
myClass& operator+(const myClass& value) {
setInt(toInt() + value.toInt());
return *this;
}
private:
std::vector<bool> bit;
};
int main() {
myClass c(10);
myClass d(20);
std::cout << "c=" << c.toInt() << "\n";
std::cout << "d=" << d.toInt() << "\n";
std::cout << "Sum=" << (c + d).toInt() << "\n";
}
`myClass c = a+b;`
Since a and b are both declared as Integer, this line will call operator+(const Integer& x, const Integer& y) or Integer::operator+(const Integer& x). The only way it will call myClass::operator+(const myClass& rhs) is if you have a conversion constructor myClass::myClass(const Integer& i).
I got this project where I have to overload the i/o operators to read and write polynomials. Unfortunately I can't seem to get it to work.
I have the header file:
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <iostream>
using namespace std;
class Polynomial
{
public:
Polynomial();
Polynomial(int degree, double coef[]);
int degree;
double coef[ ];
friend istream& operator>>(istream&,Polynomial& );
friend ostream& operator<<(ostream&,const Polynomial&);
virtual ~Polynomial();
};
#endif // POLYNOMIAL_H
and the cpp file:
#include "Polynomial.h"
#include<iostream>
#include<string>
using namespace std;
Polynomial::Polynomial()
{
//ctor
}
Polynomial::~Polynomial()
{
//dtor
}
Polynomial::Polynomial(int d, double c[])
{
degree = d;
double coef [degree+1];
for(int i = 0; i < d+1; i++)
{
coef[i] = c[i];
}
}
istream& operator>>(istream& x, const Polynomial& p)
{
cout<<"The degree: ";
x>>p.degree;
for(int i = 0; i < p.degree+1; i++)
{
cout<<"The coefficient of X^"<<i<<"=";
x>>p.coef[i];
}
return x;
}
ostream& operator<<(ostream& out, const Polynomial& p)
{
out << p.coef[0];
for (int i = 1; i < p.degree; i++)
{
out << p.coef[i];
out << "*X^";
out << i;
}
return out;
}
In the name I am trying to read a polynomial and then to write another one:
#include <iostream>
using namespace std;
#include "Polynomial.h"
int main()
{
Polynomial p1();
cin >> p1;
int degree = 2;
double coef [3];
coef[0]=1;
coef[1]=2;
coef[3]=3;
Polynomial p(degree, coef);
cout<<p;
return 0;
}
When I run the program it just freezes and I can't seem to find the error.
Any ideas?
Polynomial::Polynomial(int d, double c[])
{
degree = d;
double coef [degree+1];
for(int i = 0; i < d+1; i++)
{
coef[i] = c[i];
}
}
Here, you create local array coef (with non-standard C++ btw) and then assign to it. Your member coeff is not initialized to anything meanigfull (and makes little sense the way it is right now in the first place).
Instead of double coef[] you should use std::vector like this:
struct polynomial {
std::vector<double> coef;
// No need for member varaible degree, vector knows its lengths
polynomial (const std::vector<double> &coeffs) : coef (coeffs) {}
};
And then define all other constructors you need to do something meaningful. Alternatively, you can leave out constructors entirely and directly assign the coefficient vector. Then you can for example functions like
int degree (const polynomial &p) {
return p.coef.size() - 1;
}
or
std::ostream &operator << (std::ostream &out, const polynomial p) {
if (p.coef.size() == 0) {
out << "0\n";
return out;
}
out << p.coeff[0];
for (int i = 1; i < p.coef.size(); ++i)
out << " + " << p.coef[i] << "*X^i";
out << "\n";
return out;
}
(1)
double coef[];
This is non-standard approach to have un-sized/dynamic-sized array on stack. You better give the array some size, OR make it a pointer and allocate memory yourself; OR use vector<double>
(2)
You are creating a local variable in constructor, which will hide the member-variable in class. If you are using pointer approach, you need to allocate it properly here in constructor. With either approach, you should initialize the (member) variable coef with zeros, ideally.
(3)
Polynomial p1();
This effectively declares a function named p1 which would return a Polynomial and not a variable of tyoe Polynomial. You may want to use:
Polynomial p1;
I am struggling with adding operations of classes Natural, Rational, Complex that represent appropriate math objects. I need that to calculate polynomial in x.
All classes inherit abstract class Number. Having all coefficients in an array of Numbers I want to calculate the polynomial. To do so I need operation of multiplying by double (x is double). x gets transformed into Rational and multiplied. This works fine. My problem is how to add classes of abstract type Number?
I can't make it work. All I get is never ending recursion in Number::add(Number) (it invokes itself instead of invoking others methods for types Natural, Rational, Complex).
#include
#include
#include
#include
#include
#include
#include
using namespace std;
class Natural;class Rational;class Complex;
class Number {
public:
virtual string toString() const = 0;
virtual Number *operator*(const Rational) const = 0;
virtual Number *add(const Natural*) const = 0;
virtual Number *add(const Rational*) const = 0;
virtual Number *add(const Complex*) const = 0;
virtual Number *add(const Number *n) const {
n->add(this);
}
};
class Natural : public Number {
friend class Complex;
int n;
public:
Natural(const Natural &s) {
n = s.n;
}
Natural(int number) : n(number) {}
string toString() const {
stringstream ss;
ss << n;
return ss.str();
}
operator Rational() const;
operator Complex() const;
operator int() const {
return n;
}
Number *operator*(const Rational r) const;
Number *add(const Natural* number) const {
return new Natural(n + number->n);
}
Number *add(const Rational*) const;
Number *add(const Complex*) const;
};
class Rational : public Number {
friend class Natural;
int numerator, denominator;
void divideByGCD() {
int a = numerator, b = denominator;
//cout << a << ' ' << b << ' ';
if(a < b) {
int temp = a;
a = b;
b = temp;
}
while (b > 0) {
int r = a % b;
a = b; b = r;
//cout << r << endl;
}
numerator /= a;
denominator /= a;
//cout << a << endl;
}
public:
Rational() {}
Rational(const Rational &s) {
numerator = s.numerator;
denominator = s.denominator;
}
Rational(int n, int d) {
if(d == 0) throw new runtime_error("denominator equals 0");
if(d < 0) {
numerator = -n;
denominator = -d;
} else {
numerator = n;
denominator = d;
}
divideByGCD();
}
Rational(double d) {
int i = 0, mul = 1;
int r = d-floor(d);;
while(r!=0) {
i++; mul *= 10;
r = 10*r-floor(10*r);
}
numerator = (int)mul*d;
denominator = mul;
divideByGCD();
}
string toString() const {
stringstream ss;
ss << numerator;
if(denominator > 1) ss << '/' << denominator;
return ss.str();
}
operator const Complex() const;
operator const double() const {
return (double)numerator/denominator;
}
Number *operator*(const Rational r) const {
return new Rational(numerator*r.numerator, denominator*r.denominator);
}
Number *add(const Rational* r) const {
return new Rational(numerator*r->denominator+r->numerator*denominator, denominator*r->denominator);
}
Number *add(const Natural*) const;
Number *add(const Complex*) const;
};
class Complex : public Number {
friend class Rational;
double real, imaginary;
static const double radius = 10;
public:
Complex() {}
Complex(const Complex &s) {
real = s.real;
imaginary = s.imaginary;
}
Complex(const double r, const double im) : real(r), imaginary(im) {}
string toString() const {
stringstream ss;
ss << real;
if(imaginary != 0) ss << '+' << imaginary << 'i';
return ss.str();
}
Number *operator*(const Rational r) const;
Number *add(const Complex* c) const {
return new Complex(real + c->real, imaginary + c->imaginary);
}
Number *add(const Natural*) const;
Number *add(const Rational*) const;
};
Natural::operator Rational() const {
return Rational(n,1);
}
Natural::operator Complex() const {
return Complex(n, 0);
}
Rational::operator const Complex() const {
return Complex((double)numerator/denominator, 0);
}
Number *Natural::operator*(const Rational r) const {
return new Rational(n*r.numerator, r.denominator);
}
Number *Complex::operator*(const Rational r) const {
return new Complex(real*(double)r, imaginary*(double)r);
}
Number *Natural::add(const Rational *r) const {
if(r->denominator == 1) return new Natural(n+r->numerator);
else return new Rational(n*r->denominator,r->denominator);
}
Number *Natural::add(const Complex *c) const {
return c->add(this);
}
Number *Rational::add(const Natural *n) const {
return n->add(this);
}
Number *Rational::add(const Complex *c) const {
return new Complex(c->real+(double)*this, c->imaginary);
}
Number *Complex::add(const Natural *number) const {
return new Complex(real+number->n, imaginary);
}
Number *Complex::add(const Rational *r) const {
return r->add(this);
}
Number *poly(double x, Number *a[], unsigned int size) {
if(size == 1) return a[0];
else return a[0]->add((*poly(x, a+1, size-1))*Rational(x));
}
int main() {
cout << (Natural(5)*(Rational)2.0)->toString() << endl;
Number *coefs[] = {new Natural(5), new Natural(6)};
cout << poly(2, coefs, 2) << endl;
}
How should I fix Number::add(Number) so that while invoking add on object of type Number program itself figure out which of virtual method add to choose?
This is known as multi-dispatch. Here are some links to look at
Multiple_dispatch
best multimethods implementation
I think the problem is:
virtual Number *add(const Number *n) const {
n->add(this);
}
If you multiply a Rational by a Natural that is stored in a Number *, it can't polymorphicly upcast the Number * to a Natural *. I agree w/g-makulik in that references/values make a lot more sense here, as you are leaking memory all over the place. Remove support for Number + Number. Also, if I add a Natural and a Rational together, I get a Number * back, but what kind of number is it? I think the architecture needs a bit more thought; I might get rid of the base class pure virtual methods entirely (except maybe toString). For example:
class Number
{
public:
virtual string toString() = 0;
};
class Rational : public Number
{
string toString() {...}
// forget 'add', use operators
Rational operator+(const Rational & _rhs) const {Rational ret; ...; return ret;}
Rational & operator+=(const Rational & _rhs) const {...; return *this;}
...
}
Edit
For a quick fix, I think you just need to get rid of virtual Number *operator*(const Rational) const = 0;, and replace it with a version for each sub-class (e.x., Rational * operator*(const Natural) const)
Or, add an enumerated member variable to Number to keep track of the type:
enum Type { NATURAL, RATIONAL, ...}
Type mType;
or use RTTI, such that you can selectively choose the right add method in Number::add:
Number * add(Number * _rhs)
{
if(_rhs->mType == RATIONAL)
return this->add((Rational *)_rhs);
...
}
it looks kinda sloppy, but it will work
it looks like Visitor pattern is what I've been looking for. I wanted to have functions accept and visit in the same class. I believe my mistake was to give them the same name.
When I try to use this line in main.cpp:
m3.array = m1.array+m2.array;
where m3, m1, and m2 are all objects of a class type Matrix, with an int[3][3] array -
I keep getting an error that deals with an incompatible assignment of type int[3][3] and int[3][3] to opperand '+'. I don't know the exact error, because I'm not at a computer to compile the program.
Here's the matrix.cpp I have:
#include <iostream>
#include <string>
#include "matrix.h"
using namespace std;
Matrix::Matrix()
{
m1.array = 0;
}
istream& opeator >>(istream& inp, Matrix& m1)
{
int i, j;
for (i = 0; i < 3;i++)
{
for (j=0; j < 3;j++)
{
inp >> m1.array[i][j];
}
}
return inp;
}
ostream& operator <<(istream& outp, Matrix& m1)
{
int i, j;
for (i = 0;i<3;i++)
{
for (j = 0;j<3;j++)
{
out<<m1.array[i][j]<<" "<<endl;
}
}
return outp;
}
Matrix operator + (const Matrix& m1, const Matrix& m2)
{
Matrix answer;
int i,j;
for (i = 0;i<3;i++)
{
for (j = 0;j<3;j++)
{
answer.array[i][j] = m1.array[i][j] + m2.array[i][j];
}
}
return answer;
}
Matrix operator - (const Matrix& m1, const Matrix& m2)
{
Matrix answer;
int i, j;
for (i = 0;i<3;i++)
{
for (j = 0;j<3;j++)
{
answer.array[i][j] = m1.array[i][j] - m2.array[i][j];
}
}
return answer;
}
Matrix operator * (const Matrix& m1, const matrix& m2)
{
Matrix answer;
int i, j, k;
for (i = 0;i<3;i++)
{
for (j = 0; j<3;j++)
{
for (k = 0; k<3;k++)
{
answer.array[i][j] = m1.array[i][k] + m2.array[k][j];
}
}
}
return answer;
}
and the matrix.h:
#ifndef MATRIX_H
#define MATRIX_H
using namespace std;
class Matrix
{
public:
Matrix();
friend istream& operator >>(istream&, Matrix&);
friend ostream& operator <<(ostream&, const Matrix&);
friend Matrix& operator +(const Matrix&, const Matrix&);
friend Matrix& operator -(const Matrix&, const Matrix&);
friend Matrix& operator *(const Matrix&, const Matrix&);
int array[3][3];
};
#endif
Matrix operator + (const Matrix& m1, const Matrix& m2)
This tells the computer how to add two Matrix objects, good job.
m3.array = m1.array+m2.array;
m1 and m2 are Matrix objects, but m1.array is not. That is a int[3][3] object. Luckily, the fix is very very simple:
m3 = m1 + m2;
The member array is of type int[3][3]. You are trying to add those two multi-dimensional arrays which has no meaning in C++.
I assume what you really want is:
m3 = m1 + m2;
which would call your overloaded operator.
Something else that is also suspicious is that your friend declarations and your actual definitions don't match up. The return types are different.