简体   繁体   English

我的类的一个函数导致我的程序崩溃

[英]A function of my class causes my program to crash

I'm doing a simple_vector class, wich can only contain doubles, using dynamic arrays.我正在做一个 simple_vector 类,它只能包含双打,使用动态数组。 Somehow, my function push is making my program crash, and I don't know what's going on.不知怎么的,我的函数推送让我的程序崩溃了,我不知道发生了什么。 When I call it, the console stays still for a moment and then immediately ends the main.当我调用它时,控制台会停留片刻,然后立即结束主程序。

I have no idea...我不知道...

void simple_vector::push(double value){
if (length == capacity){
    expandCapacity();
}
  array[length++] = value;
}

expandCapacity() duplicates the capacity of the array and copies its content. expandCapacity() 复制数组的容量并复制其内容。

It shouldn't enter the if, considering that I'm doing the push to an empty vector.考虑到我正在推动一个空向量,它不应该输入 if。

where is the line your program crashed.你的程序崩溃的那一行在哪里。

first , assume that expandCapacity() crashing your program.首先,假设 expandCapacity() 使您的程序崩溃。 i have no idea,, because there is no information about expandCapacity() fucntion.我不知道,因为没有关于 expandCapacity() 功能的信息。 so you should ask your questrion cleary.所以你应该问清楚你的问题。

second , assume that array[length++] crahsing your program.其次,假设 array[length++] 破坏了你的程序。 then length value is bigger than your array size.那么长度值大于您的数组大小。 In additional , array start from 0 index.此外,数组从 0 索引开始。 so you declare 10 size array, then you can access array[0] ~ array[9].所以你声明了 10 个大小的数组,然后你就可以访问 array[0] ~ array[9]。

thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM