I am new to coding and currently learning C++ to get started, so please excuse my narrow knowledge and very possible mistakes in code and general C++ lingo.
I have written a class of complex numbers called ComplexNumber with member variables being double re and double im with the obvious meanings. This class has a constructor, an empty constructor, a copy constructor, a destructor etc.
Using this I am now meaning to write a class ComplexVector of vectors with entries from ComplexNumber. For now I have defined the member variables to be a vector std::vector<ComplexNumber> vec and a size int size. Additionally I would like to define a member function void print(), again with the obvious meaning. The code I have so far is:
#include <iostream>
#include <vector>
#include "complex.h"
class ComplexVector {
private:
std::vector<ComplexNumber> vec;
int size;
public:
ComplexVector(std::vector<ComplexNumber> vector, int n){ //constructor
size = n;
vec = vector;
};
ComplexVector(){ //empty constructor
size = 0;
vec = {};
};
void print() const;
~ComplexVector(); //destructor
ComplexVector(const ComplexVector& v); //copy constructor
ComplexVector addition(ComplexVector w); //
ComplexVector subtraction(ComplexVector w); // i am not worrying about these for now
ComplexVector scale(ComplexNumber z); //
};
with defnitions
void ComplexVector::print() const {
for(std::vector<ComplexNumber>::iterator it = vec.begin(); it != vec.end(); it++){
std::cout << *it << " ";
};
std::cout << std::endl;
};
ComplexVector::~ComplexVector(){
std::cout << "calling destructor" << std::endl;
};
ComplexVector::ComplexVector(const ComplexVector& v){
size = v.size;
vec = v.vec;
};
Here is where I am getting a compiling error: in the definition of the print my compiler tells me
error: no viable conversion from '__wrap_iter<std::__1::vector<ComplexNumber, std::__1::allocator<ComplexNumber>
>::const_pointer>' to '__wrap_iter<std::__1::vector<ComplexNumber, std::__1::allocator<ComplexNumber> >::pointer>'
for(std::vector<ComplexNumber>::iterator it = vec.begin(); it != vec.end(); it++){
which I am not quite sure how to deal with. I have read something about certain member functions having to be defined for ComplexNumber to be used in an std::vector. I also played around with defining iterators inside of ComplexVector but this did not solve the problem. From my (to be fair very narrow) perspective there should be an iterator for vec and also corresponding begin()and end() functions. I have checked if I am passing arguments of the right type a thousand times, but I must be overlooking something.
One more thing to note is that I am very much aware that this is probably an extremely inefficient way to define a class like this. I have looked at multiple examples which used pointers to an array as the main member variable. I am surely going to implement something of that type next, but for now I want to understand where the mistake in my current code is. So thanks in advance for any answers.
One side question: I don't think I have understood the concept of a destructor very well (to be fair I haven't spent much time reading about it yet), but if anyone has a quick intuition about them, which he/she wants to share, that would be highly appreciated.
Also if you have any comments on style and/or other improvements of my code, I would appreciate it if you could share them.
Thank you!
Aucun commentaire:
Enregistrer un commentaire