【发布时间】:2014-11-21 17:03:48
【问题描述】:
我正在尝试创建一个类似于 C++ 中内置矢量类的类。我已尝试按照 Walter Savitche 教科书中的所有说明进行操作,但无法使其正常工作。
代码是使用 Code::Blocks IDE 编写并使用 gcc 编译器编译的。
我认为我缺少的是数组参数和指向数组的指针之间的关系。 这就是我对普通变量的理解:
int *p1, *p2, *p3, *p4, a;
a = 5; // variable of type int with value 5
p1 = &a; // p1 now points to the value 5
p2 = p1; // p2 now also points to the value of a
p3 = new int; // p3 points to an anonamous variable of type int with undefined value
*p3 = *p1 // value of variable changed to the value of a, namely 5, but doesn't point to a
p4 = new int; // p4 points to an anonamous variable of type int with undefined value
*p4 = 5; // value of variable changed to 5
p4 = p1 // p4 now also points to the value of a
这是我对数组和指向数组的指针基本上不了解的地方
int *p1, *p2, *p3, *p4, a[3] = {4, 5, 6}; // a points to the first indexed element of the array, namely 4
p1 = a; // p1 points to the exactly the same thing as a
p2 = new int[3]; // p2 points to an array of base type int with undefined values
p2[0] = 8; // is this the correct usage? is p2 "dereferenced"
p2[1] = 9;
p2[2] = 10;
p2[2] = p1[2]; // again is this correct? is the third element of the array pointed to by p2 now equal to 6?
*p3 = a // what does this mean?
p4 = new int[4]; // p4 points to an array of base type int with undefined values
p4[0] = p2[0];
p4[1] = p2[1];
p4[2] = p2[2];
p4[3] = 3
p2 = p4 // p2 now points to p4, but what happens to the array p2 was pointing to?
delete [] p2; // does this destroy the pointer and the array it is pointing to or just one or the other?
为了完整起见,我的班级定义如下:
class VectorDouble
{
public:
// constructors
VectorDouble(); // default constructor
VectorDouble(int init_count); // user specified
VectorDouble(const VectorDouble& vd_object); // copy constructor
// destructor
~VectorDouble();
// accessors
int capacity_vd(); // get max_count
int size_vd(); // get amt_count
double value_at(int index); // get value of "value" at index i
// mutators
void push_back_vd(double put_at_end); // insert new element at end of "value"
void reserve_vd(int incr_capacity); // set max_count
void resize_vd(int incr_size); // set amt_count
void change_value_at(double d, int index); // set value of "value" at index i
// overloaded =
void operator =(const VectorDouble& vd_object_rhs);
// other
friend bool operator ==(VectorDouble vd_object1, VectorDouble vd_object2);
private:
double *value; // pointer that points to array of type double
int max_count; // the memory allocated to the array
int amt_count; // the amount of memory in use
};
而麻烦的功能是:
void VectorDouble::push_back_vd(double put_at_end)
{
double *temp;
if(amt_count == max_count)
max_count += 1;
temp = new double[max_count];
for(int i = 0; i < amt_count; i++)
temp[i] = value[i];
amt_count += 1;
temp[amt_count] = put_at_end;
value = temp;
}
成员函数似乎只是插入 0.0 而不是用户输入,我不知道为什么......
主要:
VectorDouble vec1(10);
double dd;
cout << "Enter 3 doubles to vec1:\n";
for(int i = 0; i < 3; i++)
{
cout << i << ": ";
cin >> dd;
vec1.push_back_vd(dd);
}
cout << "The variables you entered were:\n";
for(int i = 0; i < 3; i++)
cout << i << ": " << vec1.value_at(i) << endl;
我输入:
12.5 16.8 15.2
我回来了:
0 0 0
我修好了!唯一的问题是错误非常简单。很抱歉浪费大家的时间,但感谢大家,我确实学到了很多!
错误是我放置了amt_count += 1;,我习惯于从1 而非0 开始索引的数组(我已经用R 语言编写了很多代码)。解决了内存泄漏的更正代码是:
void VectorDouble::push_back_vd(double put_at_end)
{
double *temp;
if(amt_count == max_count)
max_count += 1;
temp = new double[max_count];
for(int i = 0; i < amt_count; i++)
temp[i] = value[i];
temp[amt_count] = put_at_end;
amt_count += 1;
delete [] value;
value = temp;
}
【问题讨论】:
-
为什么你认为你“不能让它工作”?出了什么问题?
-
指针从不指向值。它指向一个object(它有一个值或者是不确定的,但没关系),在一个对象后面,无处(
nullptr),或者像任何其他未初始化的对象一样是不确定的。 -
你的函数
push_back_vd确实在泄漏内存,你需要删除之前分配的空间。所以你需要在调用value = temp之前调用delete[] value;(当然是在你复制旧值之后) -
"匿名变量" - 我假设您的意思是匿名的。 C++ 没有这样的东西。你创建了一个堆变量
-
如果我使用
delete [] value;,这会不会搞砸一切,因为我实际上将删除一个私有成员变量?还是这个动作只发生在成员函数的范围内?
标签: c++ arrays class pointers vector