C++ 代码中关于编写与指针数组相同的类的错误?
大家好,我有以下代码C++ 代码中关于编写与指针数组相同的类的错误?,c++,C++,大家好,我有以下代码 在PointArray.h #ifndef POINTARRAY_H_INCLUDED #define POINTARRAY_H_INCLUDED template <typename T> class PointArray { private: int nSize; T *array[]; public: PointArray(); PointArray(const T *points[],const int size);
在
PointArray.h
#ifndef POINTARRAY_H_INCLUDED
#define POINTARRAY_H_INCLUDED
template <typename T>
class PointArray
{
private:
int nSize;
T *array[];
public:
PointArray();
PointArray(const T *points[],const int size);
PointArray(const PointArray &pv);
~PointArray();
int * get(const int position);//is same to array[position]
const int * get(const int position) const;//is same to array[position]
const int getSize() const;//get size of array
};
#endif // POINTARRAY_H_INCLUDED
在main.cpp中
#include "PointArray.h"
#include<iostream>
#include <assert.h>
using namespace std;
template<class T>
PointArray<T>::PointArray(const T *points[],const int size)
{
nSize=size;
for (int i=0;i<size;i++)
{
array[i]=new T;
*array[i]=*points[i];
}
}
template<class T>
PointArray<T>::PointArray()
{
nSize=0;
}
template<class T>
PointArray<T>::PointArray(const PointArray &pv)
{
nSize=pv.getSize();
for (int i=0;i<nSize;i++)
{
array[i]=new T;
*array[i]=*(pv.get(i));
}
}
template<class T>
const int PointArray<T>::getSize() const
{
return nSize;
}
template<class T>
PointArray<T>::~PointArray()
{
delete[] array;
nSize=0;
}
template<class T>
int * PointArray<T>::get(const int position)
{
assert(position>-1 && position<nSize);
return array[position];
}
template<class T>
const int * PointArray<T>::get(const int position) const
{
return array[position];
}
#include<iostream>
#include "PointArray.h"
#include "PointArray.cpp"
using namespace std;
int main()
{
int x=22;
int y=3;
const int * a[2]={&x,&y};
PointArray<int> p;
PointArray<int> p2(a,2);
PointArray<int> p3(p2);
cout << p.getSize() << endl;
cout << p2.getSize() << endl;
cout << p3.getSize() << endl;
return 0;
}
但产出是有限的
9244616
9244600
2
当我再次运行它时,9244616
,9244600
被更改。问题是什么?问题是您将cpp文件包含在主文件中并在其中实现,模板实现应该在h文件中完成。 您的h文件应为:
#ifndef POINTARRAY_H_INCLUDED
#define POINTARRAY_H_INCLUDED
template <typename T>
class PointArray
{
private:
int nSize;
T *array[];
public:
PointArray()
{
nSize=0;
}
PointArray(const T *points[],const int size)
{
nSize=size;
for (int i=0;i<size;i++)
{
array[i]=new T;
*array[i]=*points[i];
}
}
PointArray(const PointArray &pv)
{
nSize=pv.getSize();
for (int i=0;i<nSize;i++)
{
array[i]=new T;
*array[i]=*(pv.get(i));
}
}
~PointArray()
{
delete[] array;
nSize=0;
}
int * get(const int position)
{
assert(position>-1 && position<nSize);
return array[position];
}
const int * get(const int position) const
{
return array[position];
}
const int getSize() const { return nSize;}
};
#endif // POINTARRAY_H_INCLUDED
是内存泄漏,因为您的对象不是数组,但您试图定义指向数组的指针,所以应该将其更改为
T** array
然后在数组第一维上的析构函数循环,然后执行delete[]array
您的问题是
T *array[];
定义一个没有元素的指针数组。然后尝试访问它的(不存在的)元素,这会导致未定义的行为
另一个未定义的行为是在析构函数中
delete[] array;
您应该只删除[]
属于新[]
-ed的内容。数组
不是。相反,您应该循环所有数组元素(如果有)并删除它们中的每一个
最好的解决方案是使用std::vector
而不是数组。如果您坚持使用数组,请将T*array[]
更改为T**array
,在构造函数中为其分配内存
array = new T*[size];
并在析构函数中删除所有数组元素,然后再删除数组本身。在中将nSize初始化为0constructor@Abhijit卡丹,我不明白你的话,虽然你没有设置nSize,但后来在构造器身上看到了。通常它是在初始化列表中的首字母,而不是在正文中,因此没有注意到。奇怪的是,它的行为好像nSize没有设置为正确的值。@MM-BB:好了。。虽然我认为您需要重新检查内存分配和成员结构数组成员的大小是多少?在哪里分配?你的程序调用了未定义的行为,你能澄清一下你的答案吗,OP到底出了什么问题?(然后我将+1)@Alon您的答案是更好的,请编写代码,但问题仍然存在,虽然我很清楚,@MM-BB您是否尝试将其放入标题中?当我跑步时,它对我有效it@Alon未编辑的代码在vs2012上为我提供了0 2-但它确实发出warnings@Caribou是的,这是因为我说的另一件事,T*成员[]是一个警告,不能用零大小定义它,它应该是T**
delete[] array;
array = new T*[size];