gpt4 book ai didi

c++使用员工指针的 vector

转载 作者:太空宇宙 更新时间:2023-11-04 11:57:01 25 4
gpt4 key购买 nike

我已经创建了我的 vector 类模板,并且我已经完成了我的员工每小时类并且我领了薪水。我想使用员工指针 vector 而不是员工指针数组,我正在尝试这样做,但是当我运行它时它就中断了,而我没有列出任何错误。另外,我使用了 at 函数,例如( payroll.at(i)->writeFile(out); )来访问元素,但我没有知道出了什么问题。有什么建议吗?谢谢

这是我的代码:myvector 类模板:

#include <iostream>
#include <string>
#include <cassert>
#include <algorithm>

const int CAPACITY = 4;
template <class T>
class MyVector {
public:

MyVector();
MyVector( int size);
MyVector( int size, const T & initial);
MyVector(const MyVector<T> & v);
~MyVector();

int capacity() const;
int size() const;
void push_back(const T & value);
//T & operator[](unsigned int index);
MyVector<T> & operator=(const MyVector<T> &);
void clear();
T at(int i);

friend ostream& operator<<(ostream &out, const MyVector<T>& );


private:
int applied;
int my_size;
int my_capacity;
T * buffer;
T * daArray;
};

template<class T>
MyVector<T>::MyVector()
{
my_capacity = 0;
my_size = 0;
buffer = 0;
applied = 0;
}

template<class T>
MyVector<T>::MyVector(const MyVector<T> & v)
{
my_size = v.my_size;
my_capacity = v.my_capacity;
buffer = new T[my_size];
for ( int i = 0; i < my_size; i++)
buffer[i] = v.buffer[i];
}

template<class T>
MyVector<T>::MyVector(int size)
{
my_capacity = size;
my_size = size;
buffer = new T[size];
}

template<class T>
MyVector<T>::MyVector( int size, const T & initial)
{
my_size = size;
my_capacity = size;
buffer = new T [size];
for (unsigned int i = 0; i < size; i++)
buffer[i] = initial;
//T();
}

template<class T>
MyVector<T> & MyVector<T>::operator = (const MyVector<T> & v)
{
delete[ ] buffer;
my_size = v.my_size;
my_capacity = v.my_capacity;
buffer = new T [my_size];
for (int i = 0; i < my_size; i++)
buffer[i] = v.buffer[i];
return *this;
}

template<class T>
void MyVector<T>::push_back(const T & i)
{
if (my_capacity == 0)
{
my_capacity = 1;
my_size = 1;
applied= 0;
buffer = new T[1];
buffer[0] = i;
}
else
{
if (applied+1 == my_capacity)
{
int newCapacity = my_capacity * CAPACITY;
daArray = new T[newCapacity];
for (int i = 0; i < my_size; i++)
{
daArray[i] = buffer[i];
}
my_capacity = newCapacity;
delete buffer;
my_size++;
applied++;
buffer[applied] = i;
}
else
{
if (my_size == applied + 1)
my_size++;
applied++;
buffer[applied] = i;
}
}
}

template<class T>
int MyVector<T>::size()const//
{
return my_size;
}

template<class T>
int MyVector<T>::capacity()const
{
return my_capacity;
}

template<class T>
MyVector<T>::~MyVector()
{
delete[ ] buffer;
}

template <class T>
void MyVector<T>::clear()
{
my_capacity = 0;
my_size = 0;
buffer = 0;
}

template <class T>
T MyVector<T>::at(int i)
{
if (i < 0 || i > my_size -1)
{
string error = "Index is undefined";
throw error;
}
return buffer[i];
}

template <class T>
ostream& operator<<(ostream &out, const MyVector<T>& v)
{
for (unsigned i = 0; i < v.size(); i++)
{
out << v[i] << " ";
}
return out;
}

主要

int main() {
MyVector< employee*> payroll;
payroll.push_back(new Hourly ("H. Potter", "Privet Drive", "201-9090", 40, 12.00));
payroll.push_back(new Salaried ( "A. Dumbledore", "Hogewarts", "803-1230", 1200));

ofstream out;
out.open(file);

if (out.fail()) {
cout<<" could not open the file"<<endl;
system("PAUSE");

}

for (int i = 0; i < SIZE; i++) {
payroll.at(i)->writeFile(out);
}
out.close( );
}

最佳答案

您的 push_back 方法中存在错误。你需要这样的东西

if (applied+1 == my_capacity)
{
int newCapacity = my_capacity * CAPACITY;
daArray = new T[newCapacity];
for (int i = 0; i < my_size; i++)
{
daArray[i] = buffer[i];
}
my_capacity = newCapacity;
delete buffer;
buffer = daArray; // new line here
my_size++;
applied++;
buffer[applied] = i;
}

看看我把注释放在哪里 //new line here

关于c++使用员工指针的 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15827446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com