[英]How come my push back functions do not work and just get a seg fault
I'm Such a C++ noob and im trying to understand the code im covering better so I built this class to learn the fundamentals of overloading operators as well as push back and pop back functions ie make my own variable array. 我是个C ++新手,我想更好地理解代码,因此我建立了这个类,以学习重载运算符的基本知识以及推后弹出功能,即创建自己的变量数组。 This is not a class assignment I am just trying to teach myself the code.
这不是班级作业,我只是想自学代码。
This was actually a computer science programming test question that I did and im trying to learn from it just in case they throw something similar at me again. 实际上,这是我做过的计算机科学编程测试问题,我试图从中学习,以防万一他们再次向我扔类似的东西。 I started out in java and now doing C++.
我从Java开始,现在做C ++。
#include"IntArray.h"
#include<iostream>
using namespace std;
IntArray::IntArray()
{
size=0;
capacity=1;
data = new int[capacity];
cout<<"constructor fired off"<<endl;
}
IntArray::~IntArray()
{
delete [] data;
cout<<"Destructor fired off"<<endl;
}
IntArray::IntArray (const IntArray & m)
{
size=m.size;
capacity=m.capacity;
data= new int[capacity];
cout<<"copy constructor fired off"<<endl;
}
IntArray IntArray::operator= (const IntArray & other)
{
cout<<"operator= fired off"<<endl;
if(this != &other)//comparing the addresses
{
delete [] data;
size= other.size;
capacity = other.capacity;
data = new int[capacity];
for (int index=0; index<size; index++)
{
data[index]=other.data[index];
}
}
return *this;
}
int& IntArray::operator[] (int n)
{
if(n<0||n>=size)
{
cout<<"Array not big enough"<<endl;
return n;
}
IntArray a;
return a[n];
}
IntArray& IntArray::push_back(int n)
{
data[size]=n;
size++;
if(size==capacity)
capacity=capacity*2;
return *this;
}
IntArray& IntArray::pop_back()
{
if(size>0)
size--;
}
double IntArray::average()
{
double sum=0;
int count=0;
for(int index=0; index<size; index++)
{
sum+=data[index];
count++;
}
return sum/count;
}
int IntArray::getSize()
{
return size;
}
int IntArray::getCapacity()
{
return capacity;
}
My h file 我的h文件
#ifndef INTARRAY_H_INCLUDED
#define INTARRAY_H_INCLUDED
class IntArray
{
private:
int size;
int capacity;
int *data;
public:
IntArray();
~IntArray();
IntArray (const IntArray &);
IntArray operator= (const IntArray &);
int& operator[] (int);
IntArray& push_back(int);
IntArray& pop_back();
double average();
int getSize();
int getCapacity();
};
#endif // INTARRAY_H_INCLUDED
This isn't going to help much but there is too much that is sub optimal for me to go into. 这并不会有多大帮助,但是有太多对我而言不是最理想的选择。
If this a programming exercise then you need come up with a better design really. 如果是编程活动,那么您实际上需要提出更好的设计。 I would avoid raw pointers with new and delete, look into smart pointers.
我会避免使用带有new和delete的原始指针,而要寻找智能指针。
If you actually intend to use this, I wouldn't the standard provides a type that will meet your needs. 如果您确实打算使用此类型,那么我不会为标准提供满足您需求的类型。
#include <vector>
:::
std::vector<int> i5(5); //five ints
i5.push_back(6); //now six
i5.pop_back(); //five again
i5.clear(); //non
IntArray&
IntArray&
rhs
and creates the array, but doesn't copy the elements over rhs
相同,并创建数组,但不将元素复制到 operator[]
is returning a reference to a local variable. operator[]
正在返回对局部变量的引用。 If you want to build bounds-checking into your operator[]
, you should throw an exception, not return a number. operator[]
,则应引发异常,而不是返回数字。 operator[]
is creating a new IntArray
and calling its operator[]
, creating an infinite loop. operator[]
正在创建一个新的IntArray
并调用其operator[]
,从而创建一个无限循环。 You should return data[i]
return data[i]
pop_back
which says it returns an IntArray&
pop_back
返回任何内容,它说它返回了IntArray&
push_back
, you have to check whether your array is at capacity, and if it is, resize capacity
and data
. push_back
,您必须检查阵列是否达到容量,如果需要,请调整capacity
和data
大小。 As it is now, you're never resizing your array and you're not checking first whether it needs resizing before adding the element. You never allocate additional storage in your push_back()
function. 您永远不会在
push_back()
函数中分配其他存储。 You simply double the capacity and that's all. 您只需将容量增加一倍,仅此而已。 This will not magically extend the storage for your
int
s. 这不会神奇地扩展您的
int
的存储。 You need to allocate a new block with new capacity, copy the contents to this new block and then delete the old block. 您需要分配一个具有新容量的新块,将内容复制到该新块,然后删除旧块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.