Adding two complex numbers using classes - c++

So, I have found this code in a book:
class complex
{
public:
float x,y;
complex(float a, float b) // CONSTRUCTOR
{
x=a; y=b;
}
complex sum (complex z)
{
***complex c;*** // i get the error here
c.x=x+z.x;
c.y=y+z.y;
return c;
}
};
This code is supposed to help me sum 2 complex numbers, like this:
int main ()
{
complex a(1,2),b(1,1),c; // first number in paranthesis is the real part, the
// second one is the imaginary part
c=a.sum(b) ; // c should get the value of a+b (c.x=2, c.y=3)
return 0;
}
But everytime I try to compile it I get this error:
"no matching function for call to complex::complex()"
Why? What should I do ?

You defined your own constructor, therefore the default constructor is defined as complex() = delete;. You either need your own constructor or force the default one to be created
class complex
{
public:
float x = 0;
float y = 0;
complex() = default; // Compiler will generate the default constructor
complex(float a, float b): x(a), y(b) {}
complex sum (complex z)
{
complex c;
c.x=x+z.x;
c.y=y+z.y;
return c;
}
};
Instead of creating sum member function, I would create non-member operator+
// No need to make it friend because you declared x and y as public
complex operator+(complex const& a, complex const& b) {
return complex(a.x + b.x, a.y + b.y);
}
and use it like this
complex a(3, 4), b(5, 6);
complex c = a + b;

Related

C++ float value of a local variable explanation?

I have a simple math vector struct
struct vec3d {
float x;
float y;
float z;
float w;
public:
vec3d(float a, float b, float c) { a = x; b = y; c = z; w = 1; }
vec3d() { x = 0; y = 0; z = 0; w = 1; }
};
With the following operation in a function (I am not using operator overloading)
vec3d vsubvector(vec3d& v1, vec3d& v2)
{
return vec3d(v1.x - v2.x, v1.y - v2.y,v1.z - v2.z);
}
I am using it inside the main function within a loop block like this
{
...
vec3d normal, line1, line2;
line1 = vsubvector(p[1], p[0]);
line2 = vsubvector(p[2], p[0]);
normal = vcrossproduct(line1, line2);
normal = vnormalise(normal);
...
}
Here p is an array of three vectors
Now while debugging, when I enter the block where the local variable is defined, line1.x, line1.y and line1.z are assigned a big signed float value (-107374176.0f) and they do not change after the subtract function is returned into them.
What is the reason that vsubvector function is not working?
vec3d(float a, float b, float c) { a = x; b = y; c = z; w = 1; }
Is assigning to the constructor arguments from the uninitialised member variables. Obviously wrong. You'd want to reverse the order of assignment.
Additionally, you should use the constructors initialization list to initialize members rather than the constructor body. Do this:
vec3d(float a, float b, float c) : x(a), y(b), z(c), w(1) { }
For basic types like ints or floats it makes little difference, but for user defined types it can make a big difference. It is wasteful to first let the constructor default initialize members and then subsequently assign to them in the constructor body. Also, for some types, doing so is not even possible, not all types support assignment so initialization is the only option.
Additionally, the default constructor vec3d() can delegate to the other constructor:
vec3d() : vec3d(0.f, 0.f, 0.f) {}

overloading operator for linear algebra

I want to implement the Euler method in two dimensions and I don´t want to use any library (for practice).
Therefore I want to use my own linear algebra with overloaded functions.
The two first overloads seem to work but there´s still a problem with the multiplication matrix * vector i.e a (2x2)*(2x1).
class vector{
public:
double a;
double b;
vector::vector();
vector::vector(double a, double b){
this->a = a;
this->b = b;
};
vector operator+(vector &a);
vector operator*(double factor);
vector operator*(matrix &B);
};
class matrix{
public:
double a1;
double a2;
double b1;
double b2;
matrix::matrix();
matrix::matrix(double a1, double a2, double b1, double b2) {
this->a1 = a1;
this->a2 = a2;
this->b1 = b1;
this->b2 = b2;
};
};
vector vector::operator+(vector& v){
return vector(this->a+v.a,this->b+v.b);
};
vector vector::operator*(double factor){
return vector(this->a*factor, this->b*factor);
};
vector vector::operator*(matrix& B){
vector newv(this->a*B.a1 + B.a2*b, this->a*B.a1 + B.b2*b);
return newv;
};
Errors when I compile it:
'vector vector::operator *(matrix &)' : overloaded member function not found in 'vector'
unable to resolve function overload
Since you're a bit short on details, I must fill in the gap with guesses. My guess is that you try to support something like the following:
matrix m;
vector v, w;
// fill m and v with values
w = m*v;
I'm also guessing that your matrix has the following form:
( a1 a2 )
( b1 b2 )
You now have two options: Either implement matrix-vector multiplication in the class matrix, or implement it as a free function.
If you want to put if in the matrix class, you'd change the code as follows (note that there are many more things for the code that should be changed, but those are unrelated to the question):
class vector
{
// omit the operator* for matrix, otherwise unchanged
};
class matrix
{
// all that's already in the class
vector operator*(vector const& v) const;
};
vector matrix::operator*(vector const& v) const
{
return vector(a1*v.a + a2*v.b, b1*v.a + b2*v.b);
}
// the rest of your code
If you want to make it a free function (I personally would do it that way, but I'm sure that opinion is not universally held), you'd write
class vector
{
// omit the operator* for matrix, otherwise unchanged
};
class matrix
{
// completely unchanged
};
vector operator*(matrix const& m, vector const& v)
{
return vector(m.a1*v.a + m.a2*v.b, m.b1*v.a + m.b2*v.b);
}
// the rest of your code
I would prefer a BLAS library as an implementation in long term against reinvent the wheel again for basic matrix operations. Additionally some of the BLAS libraries are multithreaded and/or GPU based; they are widely used and tested.
In terms of design I would introduce functions what could be easily implemented by BLAS against operators:
C = α * A + β * B
C = α * A * B + β *C
So as a header:
...
bool blasAdd(const double alfa_, const Matrix& A_, const double beta_, const Matrix& B_, Matrix& C_);
bool blasMultiply(const double alfa_, const Matrix& A_, const Matrix& B_, const double beta_, Matrix& C_);
...
In this way your code automatically could be optimized for BLAS.
My favorite BLAS implementation is Intel MKL, but there are also many free BLAS implementations in the market (e.g.: boost BLAS).

Undefined reference to a class in C++

I have a code::blocks project, called Test.cbp. It has three files, main.cpp in the Test directory, and jAlg.cpp and jAlg.h, both in the jAlg directory.
main.cpp
#include <iostream>
#include "jAlg.h"
int main(){
jVector bob;
jMatrix ann;
bob.display();
return 0;
}
jAlg.cpp (summary)
class jMatrix{
public:
//The four entries of a 2x2 square matrix:
//[a b]
//[c d]
double a, b, c, d;
//Constructor
jMatrix(double a_val = 1, double b_val = 0,
double c_val = 0, double d_val = 1){
a = a_val;
b = b_val;
c = c_val;
d = d_val;
}
//Several other functions and operator overloads
};
class jVector{
public:
//The two entries of a 2D vector
//[x]
//[y]
double x, y;
//Constructor
jVector(double x_val = 0,
double y_val = 0){
x = x_val;
y = y_val;
}
//Several other functions and operator overloads
};
//Several other functions
//Matrix-vector multiplication
jVector operator* (jMatrix A, jVector v){
jVector result;
result.x = A.a*v.x + A.b*v.y;
result.y = A.c*v.x + A.d*v.y;
return result;
}
//More functions
jAlg.h (summary)
#ifndef JALG
#define JALG
double sqr(double x);
class jMatrix{
public:
double a, b, c, d;
jMatrix(double a_val = 1, double b_val = 0,
double c_val = 0, double d_val = 1);
//more functions
};
class jVector{
public:
double x, y;
jVector(double x_val = 0, double y_val = 0);
//more functions
};
jVector operator* (jMatrix A, jVector v);
#endif
When I build and run the project, it behaves strangely. An error is raised saying undefined reference to 'jMatrix::jMatrix(double,double,double,double)'. However, there is no error related to the jVector::jVector constructor.
As a test I went to the jAlg files and commented out the jVector operator* (jMatrix A, jVector v) function and run the project again. This time, an error is raised for both jMatrix::jMatrix and jVector::jVector. For some reason, the presence of that function makes it okay to define vectors, but not matrices.
So here's my question: why does the presence of the function jVector operator* (jMatrix A, jVector v) make it okay to define jVectors? What can I do to make it possible to define jMatrix objects?
In your jAlg.cpp file, you should only define your functions, don't declare everything again
jMatrix::jMatrix(double a_val, double b_val, double c_val, double d_val)
: a{a_val}, b{b_val}, c{c_val}, d{d_val}
{ }
// ... define other jMatrix functions similarly
jVector::jVector(double x_val, double y_val)
: x{x_val}, y{y_val}
{ }
// ... define other jVector functions
//Several other functions
jVector operator* (jMatrix A, jVector v)
{
jVector result;
result.x = A.a*v.x + A.b*v.y;
result.y = A.c*v.x + A.d*v.y;
return result;
}
In your jAlg.cpp file you shouldn't redeclare your class, but just put definitions for the functions declared from jAlg.h (do so for the other class member functions analogous):
jMatrix::jMatrix(double a_val, double b_val,
// ^^^^^^^^^ Prefix class scope
double c_val, double d_val)
: a(a_val), b(b_val), c(c_val), d(d_val) {}
Also use the member initializer list (as shown above).
The definition of the free function jVector operator* (jMatrix A, jVector v); is OK.

Structures for dummies

I'm studying structures in C++... I understood the basic concept of it and as far as I know the are meant to list a series of items in a more compact way.... however I came across an example where I don't understand what's going on:
struct cuComplex {
float r; // real part of a complex number
float i; // imaginary part of a complex number
/* !!! I DON'T UNDERSTAND FROM HERE !!! */
cuComplex( float a, float b ) : r(a), i(b) {}
float magnitude2( void ) { return r * r + i * i; }
cuComplex operator*(const cuComplex& a) {
return cuComplex(r*a.r - i*a.i, i*a.r + r*a.i);
}
cuComplex operator+(const cuComplex& a) {
return cuComplex(r+a.r, i+a.i);
}
/* !!! TO HERE. !!! */
};
It seems to me that some sort of function is defined inside but I don't understand how that is possible and how I should interpret it.
Q. Is there some reference where I can read about this in order to have a better idea of what is going on?
struct in C++ is basically the same as classes with one difference. In class defualt access specifier is private whereas in struct it's public.
So what you're seeing in that code are:-
constructors, overloaded operators +,* for you struct and a method to compute magnitude of a complex number.
This is like a class, with the exception of default member protection, and it has member variables r and i, read here for more: http://www.cplusplus.com/doc/tutorial/classes/
struct cuComplex {
float r;
float i; // imaginary part of a complex number
This is a constructor, read here for more: http://www.cplusplus.com/doc/tutorial/classes/#constructors
cuComplex( float a, float b ) : r(a), i(b) {}
This is a method or member function, if you've made it through the first like you've read about that, but if you want a more concise example read here: http://en.wikipedia.org/wiki/C%2B%2B_classes#Member_functions
float magnitude2( void ) { return r * r + i * i; }
These are overloaded operators read here for more: http://www.cplusplus.com/doc/tutorial/templates/#overloading_operators
cuComplex operator*(const cuComplex& a) {
return cuComplex(r*a.r - i*a.i, i*a.r + r*a.i);
}
cuComplex operator+(const cuComplex& a) {
return cuComplex(r+a.r, i+a.i);
}
};
If you have a specific question beyond this, you can comment on this post and I'll try to help explain.

How to evaluate a function directly?

Title may not make any sense but I dont really know how to explain this.
I have a class called polynomial and lets say I defined a polynome called p1 which is 2x+4. What I want to do is calculate p1(5) directly. I dont want anything like double calculate (polynomial) etc I want to be able to calculate my polynom with p1(x).
I hope my question is clear
Overload the function-call operator:
struct polynomial
{
double a, b;
polynomial(double m, double n) : a(m), b(n) { } // represents "a * x + b"
double operator()(double x) const
{
return a * x + b;
}
};
Usage:
polynomial p(2.5, 3.8);
double val = p(1.0);
By overloading operator() you can "call" an object just like you would call a function:
struct polynomial {
int operator()(int x)
{
/* calculate */
}
};
int main()
{
polynomial p;
int x = p(5);
}