C++自定义"List"实现不起作用
C++ Custom "List" implementation doesn't work
我试图创建一个列表容器,该容器与c中的元素访问权限类似#我现在完全迷路了,因为我的主要方法首先打印出了奇怪的数字。
RList类应该类似于:
RList<ClassName or Primitive> VariableName;
VariableName.AddData(Class or Primitive);
VariableName[IndexOfElement] get the element
VariableName.RemoveAt(IndexOfElement) remove element
你能告诉我哪里完全错了吗?
int main()
{
RList<int> Numbers;
Numbers.AddData(5);
Numbers.AddData(100);
Numbers.AddData(1500);
for (unsigned int x = 0; x < Numbers.GetLength(); x++)
{
cout << Numbers[0] << endl;
}
cin.get();
return 0;
}
这是头文件。我读到,如果你使用模板,你必须把所有的东西都放在标题中。
#ifndef RList_H
#define RList_H
#include <new>
template <class T> class RList
{
private:
unsigned int m_Length;
T* ListObject;
void AllocateNew(T obj);
void RemoveIndex(unsigned int N);
public:
RList();
~RList();
void AddData(T obj);
void RemoveAt(unsigned int N);
unsigned int GetLength() { return m_Length; }
T operator[](unsigned int N){if (N < m_Length && N >= 0) {return (ListObject[N]);} return NULL; }
};
template <class T>
RList<T>::RList()
{
this->m_Length = 0;
}
template <class T>
RList<T>::~RList()
{
delete[] this->ListObject;
}
template <class T>
void RList<T>::AddData(T obj)
{
this->AllocateNew(obj);
this->m_Length++;
}
template <class T>
void RList<T>::RemoveAt(unsigned int N)
{
if( N < this->m_Length && N >= 0)
{
if ((this->m_Length - 1) > 0)
{
this->RemoveIndex(N);
this->m_Length--;
}
else
{
throw "Can't erase last index!";
}
}
}
template <class T>
void RList<T>::AllocateNew(T obj)
{
if (this->m_Length == 0)
{
this->ListObject[0] = obj;
}
else
{
T* NewListObject = new T [this->m_Length + 1];
for (unsigned int x = 0; x < this->m_Length; x++)
{
NewListObject[x] = this->ListObject[x];
}
NewListObject[this->m_Length] = obj;
delete [] ListObject;
this->ListObject = NewListObject;
delete [] NewListObject;
}
}
template <class T>
void RList<T>::RemoveIndex(unsigned int N)
{
T* NewListObject = new T [this->m_Length - 1];
for (int x = 0; x < this->m_Length -1; x++)
{
if (x != N)
{
NewListObject[x] = this->ListObject[x];
}
}
delete [] ListObject;
this->ListObject = NewListObject;
}
#endif // RList_H
很多问题:
- 构造函数应初始化所有成员
- 未实现三条规则(在拥有的指针上(
- 可怕的间距(您需要更好地格式化代码(
- 所有数组内存分配都是错误的
分配:
template <class T>
void RList<T>::AllocateNew(T obj)
{
if (this->m_Length == 0)
{
// This will not work as you have not allocated the area for ListObjects.
// I don't think this is a special case. You should have allocated a zero
// length array in the constructor then then else part would have worked
// like normal when adding the first element.
this->ListObject[0] = obj;
}
else
{
// OK good start
T* NewListObject = new T [this->m_Length + 1];
// Rather than do this manually there is std::copy
for (unsigned int x = 0; x < this->m_Length; x++)
{
NewListObject[x] = this->ListObject[x];
}
NewListObject[this->m_Length] = obj;
// Though unlikely there is a posability of an exception from a destructor.
// So rather than call delete on a member you should swap the member and the
// temporary. Then when the object is a good state you can delete the old one.
delete [] ListObject;
this->ListObject = NewListObject;
// Definately do NOT do this.
// as you have just stored this pointer into ListObject.
// ListObject is now pointing at free'ed memory.
delete [] NewListObject;
// So I would have done (for the last section
// std::swap(this->ListObject, NewListObject);
// ++this->m_Length;
// // now we delete the old data
// delete [] NewListObject; // (remember we swapped above)
}
}
RemoveIndex
template <class T>
void RList<T>::RemoveIndex(unsigned int N)
{
T* NewListObject = new T [this->m_Length - 1];
for (int x = 0; x < this->m_Length -1; x++)
{
if (x != N)
{
// You need to compensate for the fact that you removed one
// element (otherwise you have a hole in your new array).
NewListObject[x] = this->ListObject[x];
}
}
// Same comment as above.
// Do not call delete on a member.
// Make sure the object is a good state before doing dangerous stuff.
delete [] ListObject;
this->ListObject = NewListObject;
}
if (this->m_Length == 0)
{
this->ListObject[0] = obj;
}
您必须先分配ListObject
,然后才能执行此操作。
注意:你的实现有很多问题,你应该把它发布到代码评审中,或者查看一本书,看看如何实现一个合适的向量。
您遇到了很多问题,但这个问题肯定会使您的程序崩溃:
(在AllocateNew
结束时(:
this->ListObject = NewListObject;
delete [] NewListObject;
现在,this->ListObject
指向已释放的内存。
你能告诉我哪里完全错了吗?
您正在重新设计STL矢量类。您可以围绕它编写一个thin-wrapper来提供您想要的API,但使用类as-is可能更容易。您的示例如下:
#include <iostream>
#include <vector>
using namespace std;
int main( )
{
vector< int > Numbers;
Numbers.push_back( 5 );
Numbers.push_back( 100 );
Numbers.push_back( 1500 );
for ( unsigned int x = 0; x < Numbers.size( ); x++ )
{
cout << Numbers[x] << endl;
}
cin.get();
return 0;
}
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 双链表的自定义实现不起作用(教育)
- 为什么读取函数在 sha1 c++ 实现中不起作用?
- 语言分析的二叉树实现 - 子节点:不起作用
- 在C++中实现二叉搜索树,搜索不起作用.尝试打印节点的元素会导致输出崩溃
- 我已经在 C++ 中实现了合并排序。它没有错误,但不知何故不起作用
- C++堆栈实现弹出函数不起作用
- 将接口与实现分离不起作用 c++
- 为什么我的网络 TCP/UDP 实现不起作用?
- C++接口(标头)和实现文件不起作用
- c++11-实现Promise的示例程序不起作用
- 复制在我的字符串中不起作用::复制实现
- 在C++中实现方法时继承不起作用
- 不同类的成员列表实现不起作用
- Wt(机智).关于Javascript实现不起作用的一无所知
- 带有私有实现的dllexport纯虚拟类不起作用
- 在 c++ 中实现静态构造函数不起作用
- C++自定义"List"实现不起作用
- 自动推导在作为 Lambda 实现时不起作用。错误:扣除'auto'之前