1

作为练习,我正在尝试创建一个 myArray 类,它充当简化的数组类。这是我的标题:

#ifndef myArray_h
#define myArray_h

typedef double ARRAY_ELEMENT_TYPE;

class myArray {

public:
//--constructors
    myArray(int initMax);
    // post: Allocate memory during pass by value

    myArray(const myArray & source);
    // post: Dynamically allocate memory during pass by value

//--destructor
    ~myArray();
    // post: Memory allocated for my_data is deallocated.

//--modifier
    void set(int subscript, ARRAY_ELEMENT_TYPE value);
    // post: x[subscript] = value when subscript is in range.
    //       If not, an error message is displayed.

//--accessor
    ARRAY_ELEMENT_TYPE sub(int subscript) const;
    // post: x[subscript] is returned when subscript is in range.
    //       If not, display an error message and return [0].

private:
ARRAY_ELEMENT_TYPE* my_data;
int my_capacity;
};
#endif

这是我的实现:

#include "myArray.h"
#include <iostream>
#include <cstring>

using namespace std;

typedef double ARRAY_ELEMENT_TYPE;

//--constructors
myArray::myArray(int initMax)
{
my_capacity = initMax;
}

myArray::myArray(const myArray & source)
{
int i;
my_data = new ARRAY_ELEMENT_TYPE[source.my_capacity];

for(i=0; i < my_capacity; i++)
    my_data[i] = source.sub(i);
}

//--destructor
myArray::~myArray()
{
delete [ ] my_data;
}

//--modifier
void myArray::set(int subscript, ARRAY_ELEMENT_TYPE value)
{
if(subscript > my_capacity - 1)
{
    cout << "**Error: subscript " << subscript << " not in range 0.." << my_capacity-1 << ". The array is unchanged." << endl;
}
else
    my_data[subscript] = value;
}

//--accessor
ARRAY_ELEMENT_TYPE myArray::sub(int subscript) const
{
if(subscript >= my_capacity)
{
    cout << "**Error: subscript " << subscript << " not in range 0.." << my_capacity-1 << ". Returning first element." << endl;
    cout << my_data[0];
}
else
{
    return my_data[subscript];
}
}

我将其用作测试驱动程序:

#include <iostream>
using namespace std;
typedef double ARRAY_ELEMENT_TYPE;
#include "myArray.h"

void show (const myArray & arrayCopy, int n)
{
for(int j = 0; j < n; j++)
    cout << arrayCopy.sub(j) << endl;
}

int main()
{
int n = 6;
myArray a(6);
a.set(0, 1.1);
a.set(1, 2.2);
a.set(2, 3.3);
a.set(3, 4.4);
a.set(4, 5.5);
a.set(5, 6.6);
show(a, n);
cout << a.sub(11) << endl;
a.set(-1, -1.1);

return 0;
}

问题是,当我运行它时,我什么也得不到,然后是“按任意键继续...”提示。怎么了?

4

3 回答 3

1

构造myArray函数不会为my_data. 第一次调用时set,它会尝试写入未初始化的指针。这会导致未定义的行为,但可能会发生崩溃。

您应该将构造函数更改为

myArray::myArray(int initMax)
{
    my_capacity = initMax;
    my_data = new ARRAY_ELEMENT_TYPE[my_capacity];
}

您还可以考虑代码的其他几个问题

在'set'中,测试

if(subscript > my_capacity - 1)

应该

if(subscript < 0 || subscript > my_capacity - 1)

或者您可以将subscript参数更改为具有 type unsigned int

sub中,该行cout << my_data[0];应该是return my_data[0];

于 2013-08-16T18:08:25.577 回答
1
myArray::myArray(int initMax)
{
my_capacity = initMax;
my_data = new ARRAY_ELEMENT_TYPE[my_capacity]; //You missed this
}
于 2013-08-16T18:10:45.307 回答
0

除了在当前实现中缺少分配之外,您还在动态分配内存。简单的数组类型不需要在堆上分配。该std::array系列完全符合您的要求。我敦促您以示例为例(如果这只是一个学术练习)。如果这是用于生产代码库,请使用已经编写和测试的代码。

http://en.cppreference.com/w/cpp/container/array

于 2013-08-16T18:34:11.700 回答