Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 堆损坏和其他问题_C++_Oop_Heap Memory - Fatal编程技术网

C++ 堆损坏和其他问题

C++ 堆损坏和其他问题,c++,oop,heap-memory,C++,Oop,Heap Memory,我刚开始学习OOP,我的第一个项目有一些问题。它并不是每次都运行,但运行时会显示正确的值。 当它不运行时,问题似乎是cout,tough I重载re=0;} 努马鲁综合体(努马鲁综合体建筑和北){ 这->re=n.re; 这个->im=n.im; } ~Numar_Complex()=默认值; 双模(); Numar_复数运算符+(Numar_复数常量和obj){ 努马鲁复合体; res.re=此->re+obj.re; res.im=此->im+obj.im; 返回res; } Numar_复

我刚开始学习OOP,我的第一个项目有一些问题。它并不是每次都运行,但运行时会显示正确的值。 当它不运行时,问题似乎是cout,tough I重载re=0;} 努马鲁综合体(努马鲁综合体建筑和北){ 这->re=n.re; 这个->im=n.im; } ~Numar_Complex()=默认值; 双模(); Numar_复数运算符+(Numar_复数常量和obj){ 努马鲁复合体; res.re=此->re+obj.re; res.im=此->im+obj.im; 返回res; } Numar_复数运算符*(Numar_复数常量和obj){ 努马鲁复合体; res.re=this->re*obj.re-this->im*obj.im; res.im=this->re*obj.im+this->im*obj.re; 返回res; } friend ostream&operator>obj.im; 返回; } void运算符=(const Numar_Complex&x){ 这->re=x.re; 这->im=x.im; } }; 类向量_复合体:公共Numar_复合体{ 内伦; 努马鲁复合体*v; 公众: 向量_复数(); 向量复形(Numar复形常数&x,int n); 向量复形(向量复形&w); ~Vector_Complex()=默认值; double*获取模块的向量(); int Get_Len(){ 回程透镜; } 无效排序(); Numar_复数和向量(); Numar_复数乘积标量(向量_复数常数&a,向量_复数常数&b); friend ostream&operator w.v[i]; 返回; } }; int main() { 向量_复合向量; cin>>Vec;
您可以默认构造一个
Vector\u Complex
对象,而您的默认构造函数会这样做

Vector_Complex::Vector_Complex() {
    len = 0;
    v = new Numar_Complex[0];
}
然后从一个流读取到
Vector\u Complex
对象,该对象

friend istream& operator >>(istream& in, Vector_Complex & w) {
    in >> w.len;
    for (int i = 0; i < w.len; i++)
        in >> w.v[i];
    return in;
}
friend-istream&operator>>(istream&in,Vector\u Complex&w){
在>>w.len;
对于(int i=0;i>w.v[i];
返回;
}

现在,由于默认构造了
Vector\u Complex
对象,因此
v
变量将是指向零元素“数组”的指针。这意味着
w.v[i]
对于
i
的任何值都无效。写入该变量(使用
in>>w.v[i]
)导致未定义的行为。

您默认构造一个
Vector\u Complex
对象,而您的默认构造函数会

Vector_Complex::Vector_Complex() {
    len = 0;
    v = new Numar_Complex[0];
}
然后从一个流读取到
Vector\u Complex
对象,该对象

friend istream& operator >>(istream& in, Vector_Complex & w) {
    in >> w.len;
    for (int i = 0; i < w.len; i++)
        in >> w.v[i];
    return in;
}
friend-istream&operator>>(istream&in,Vector\u Complex&w){
在>>w.len;
对于(int i=0;i>w.v[i];
返回;
}

现在,由于默认构造了
Vector\u Complex
对象,因此
v
变量将是指向零元素“数组”的指针。这意味着
w.v[i]
对于
i
的任何值都无效。写入该变量(使用
in>>w.v[i]
)导致未定义的行为。

您的设计似乎是错误的。
Vector\u Complex
是否真的应该是
Numar\u Complex
?我不这么认为。是的,应该是这样,这没有意义……复数容器本身不是复数。您的设计似乎是错误的。
Vector\u Complex
真的应该是
Numar\u Complex吗x
?我不这么认为。是的,这应该是没有意义的…复数容器本身不是复数。谢谢。这就是问题所在。放大!谢谢。这就是问题所在。放大!