c++使用员工指针的 vector

标签 c++

我已经创建了我的 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/

相关文章:

c++ - 为什么基指针值存储在堆栈中

c++ - 在 qtcreator 的 cmake 项目中设置进程数 mpi

c++ - 由 n 个点定义的超平面

c++ - (编码)C++ 中的字符串处理 - 问题/最佳实践?

c++ - 如何使用 C++ 在 Windows 中获取应用程序数据路径?

c++ - 无法在 Windows 10/Visual C++ 2017 上使用 tensorflow C++ API tensorflow_cc.lib 链接 C++ -- undefined symbol r2.0

c++ - c++和opencv生成奇怪的图片标题

c++ - 射线与球体相交的相交问题

c++ - 模板函数类型的后期绑定(bind) C++

android - [android ndk]如何获取 Assets 文件夹中文件的绝对路径?