[英]Creating vector of custom complex number class using std::vector
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.我是编码新手,目前正在学习 C++ 以开始学习,所以请原谅我的狭隘知识和代码中很可能出现的错误以及一般 C++ 术语。
I have written a class of complex numbers called ComplexNumber
with member variables being double re
and double im
with the obvious meanings.我写了一个名为
ComplexNumber
的复数 class,其成员变量是double re
和double im
,含义很明显。 This class has a constructor, an empty constructor, a copy constructor, a destructor etc.这个 class 有一个构造函数、一个空构造函数、一个复制构造函数、一个析构函数等。
Using this I am now meaning to write a class ComplexVector
of vectors with entries from ComplexNumber
.使用这个我现在的意思是写一个 class
ComplexVector
的向量与来自ComplexNumber
的条目。 For now I have defined the member variables to be a vector std::vector<ComplexNumber> vec
and a size int size
.现在我已经将成员变量定义为一个向量
std::vector<ComplexNumber> vec
和一个 size int size
。 Additionally I would like to define a member function void print()
, again with the obvious meaning.另外我想定义一个成员 function
void print()
,同样具有明显的含义。 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这是我遇到编译错误的地方:在
print
的定义中,我的编译器告诉我
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
.我已经阅读了一些关于必须为
ComplexNumber
定义的某些成员函数才能在std::vector
中使用的内容。 I also played around with defining iterators inside of ComplexVector
but this did not solve the problem.我还尝试在
ComplexVector
中定义迭代器,但这并没有解决问题。 From my (to be fair very narrow) perspective there should be an iterator for vec
and also corresponding begin()
and end()
functions.从我(公平地说,非常狭隘)的角度来看,应该有一个
vec
的迭代器以及相应的begin()
和end()
函数。 I have checked if I am passing arguments of the right type a thousand times, but I must be overlooking something.我已经检查过我是否通过了正确类型的 arguments 一千次,但我必须忽略一些东西。
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.还有一点需要注意的是,我非常清楚这可能是一种非常低效的方式来定义这样的 class。 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!谢谢!
Edit: Here is the ComplexNumber
class编辑:这里是
ComplexNumber
class
class ComplexNumber {
private:
double re;
double im;
public:
ComplexNumber(double x, double y){
re = x;
im = y;
};
ComplexNumber(){
re = im = 0;
};
ComplexNumber(const ComplexNumber& z);
void print() const;
};
and definitions和定义
void ComplexNumber::print() const {
if(im > 0){
std::cout << re << "+" << im << "i" << std::endl;
} else if(im < 0){
std::cout << re << im << "i" << std::endl;
} else {
std::cout << re << std::endl;
};
};
ComplexNumber::ComplexNumber(const ComplexNumber& z){
re = z.re;
im = z.im;
};
And a main method:还有一个主要方法:
int main() {
std::vector<ComplexNumber> v1 = {ComplexNumber(1,2), ComplexNumber(4,2)};
int n = 2;
ComplexVector w1(v1,n);
w1.print();
return 0;
}
Your problem is here:你的问题在这里:
void ComplexVector::print() const {
for(std::vector<ComplexNumber>::iterator it = vec.begin(); it != vec.end(); it++){
std::cout << *it << " ";
};
std::cout << std::endl;
};
Notice that the function is marked const
.请注意,function 标记为
const
。 This means any members of the class are treated as const qualified inside the method (ie you are not allowed to modify them).这意味着 class 的任何成员在方法内都被视为 const 限定(即您不得修改它们)。 Thus when you call
vec.begin()
you are calling the const version of begin()
on vector.因此,当您调用
vec.begin()
时,您正在调用向量上begin()
的 const 版本。 This returns const_iterator
not an iterator
.这将返回
const_iterator
而不是iterator
。
std::vector<ComplexNumber>::iterator it = vec.begin()
^^^^^^^^ should be const_iterator
A better way to solve this is to use auto
and let the compiler work out the correct type:解决此问题的更好方法是使用
auto
并让编译器计算出正确的类型:
auto it = vec.begin()
So the function becomes:所以 function 变为:
void ComplexVector::print() const {
for(std::vector<ComplexNumber>::const_iterator it = vec.begin(); it != vec.end(); it++){
std::cout << *it << " ";
};
std::cout << std::endl;
}
or with auto:或自动:
void ComplexVector::print() const {
for(auto it = vec.begin(); it != vec.end(); it++){
std::cout << *it << " ";
};
std::cout << std::endl;
}
or you can improve this by using the new version of for()
或者您可以通过使用新版本的
for()
来改进这一点
void ComplexVector::print() const {
for(auto const& item: vec){
std::cout << item << " ";
};
std::cout << "\n";
}
Note: When your code works.注意:当您的代码有效时。 You can ask for a review on style at https://codereview.stackexchange.com
您可以在https://codereview.stackexchange.com要求对样式进行评论
Follow up based on comments: Try:根据评论跟进:尝试:
class ComplexNumber
{
......
friend std::ostream& operator<<(std::ostream& str, ComplexNumber const& data) {
data.print(); // You want to change this
// so you can pass the stream to print.
return str;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.