2013-08-16 36 views
1

作爲一個練習,我試圖創建一個類myArray,充當簡化的數組類。這裏是我的頭:C++創建原始數組類

#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; 
} 

的問題是,當我運行這個,我什麼都得不到,然後「按任意鍵繼續...」提示。出了什麼問題?

+1

不要忘了[三規則(http://stackoverflow.com/questions/4172722)。三分之二是不錯的,但是你錯過了一個複製分配操作符。 –

回答

1

構造函數myArray不會爲my_data分配內存。第一次調用set時,它會嘗試寫入未初始化的指針。這會導致未定義的行爲,但可能會導致崩潰。

你應該改變構造函數

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

有幾個與代碼的其他問題,您也可以考慮

在「設置」,測試

if(subscript > my_capacity - 1) 

應be

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

或者您可以將subscript參數更改爲unsigned int

sub,行cout << my_data[0];應該大概是return my_data[0];

+0

解決了大部分問題。現在我得到一個警告:'sub'中的所有返回路徑都不會返回一個值,但我無法弄清楚if(subscript> = my_capacity)和else之外的內容。 我在'a.set(-1,-1.1)'的最後一次調用中發生崩潰,輸出顯示爲'1.1-1。#IND'和一個堆損壞調試錯誤。 – spartanhooah

+0

我已經更新了我的答案,並注意到了造成這些問題的原因。 – simonc

+0

我錯過了最後一行,它擺脫了#IND問題。感謝您的幫助(因爲我還沒有投票)。 – spartanhooah

1
myArray::myArray(int initMax) 
{ 
my_capacity = initMax; 
my_data = new ARRAY_ELEMENT_TYPE[my_capacity]; //You missed this 
} 
0

除了當前的實施缺少你分配,你也動態地分配內存。一個簡單的數組類型不需要在堆上分配。 std::array集合完全符合您的要求。我會敦促你看看它的實施例子(如果這只是一個學術練習)。如果這是生產代碼庫,請使用已編寫和測試的內容。

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

+0

'std :: array'是靜態大小的。直到明年我們才獲得動態大小的數組和std :: dynarray,如果在編譯時不知道大小,我們需要動態分配。 (這個問題確實表明這是一個練習,所以「使用vector」也不是有用的答案)。 –

+0

我並沒有聲明使用'std :: array'或'std :: vector'(除非它用於生產代碼庫)。我很清楚他應該看看'std :: array'的實現來了解它是如何完成的。他目前的實現創建了一個不可調整大小的動態分配數組(換句話說,他知道在編譯時它有多大,但無論如何都是在堆上分配它)。 –

+0

不,當前實現將大小作爲運行時構造函數參數,而不是像'array'那樣的編譯時模板參數。直到明年,你需要動態分配來處理這個問題。 (在這個例子中,大小是一個常量;但沒有跡象表明該類應該限制在編譯時間值)。 –