2016-03-03 101 views
0

我是一個JavaScript程序員,他試圖通過將我的JavaScript類移植到C++來學習C++。我很快發現C++數組中的數組與JavaScript中的數組非常不同。在構造函數中初始化數組

我班的成員有數組屬性。我無法在構造函數中初始化數組。這是我的代碼。

class Name { 
    private: 
    unsigned int array[]; 

    public: 
    unsigned int getArray(); 
    Name(unsigned int); 
} 
//I'm defining everything outside the class because I'm told that's good practice 
unsigned int Name::getArray() { 
    return this->array; 
} 
//this is the problem 
Name::Name(unsigned int length) { 
    this->array = new unsigned int [length]; 
} 

我沒有線索如何解決最後一部分。我一直在亂搞它很多年。似乎沒有任何工作。其餘的代碼很好,我只是得到1構造函數的錯誤。

我使用g ++編譯。

+7

如果您想要一個大小在運行時決定的數組,請改用['std :: vector'](http://en.cppreference.com/w/cpp/container/vector)。 –

+2

也看看'std :: array'。見[這裏](http://en.cppreference.com/w/cpp/container/array)。 – skypjack

+3

問題:1.'new'返回一個指針,而不是一個數組。 2.你不能分配給一個數組(你正混淆賦值和初始化)。 –

回答

-1

您的代碼固定到像如C++(以及更象C):

class Name { 
    private: 
    unsigned int *array; 

    public: 
    unsigned int *getArray(); 
    Name(unsigned int); 
} 
//I'm defining everything outside the class because I'm told that's good practice 
unsigned int *Name::getArray() { 
    return this->array; 
} 
//this is the problem 
Name::Name(unsigned int length) { 
    this->array = new unsigned int [length]; 
} 

而現在的多個C++方法:

class Name { 
    private: 
    std::vector<unsigned int> array; 

    public: 
    std::vector<unsigned int> getArray(); // Do not worry about optimizations here; check RVO 
    Name(unsigned int); 
} 
//I'm defining everything outside the class because I'm told that's good practice 
std::vector<unsigned int> Name::getArray() { 
    return array; 
} 
//this is the problem 
Name::Name(unsigned int length) { 
    // this line is just to allocate the space for the array, but please keep in ming that you can simply add elements with push_back and the array will grow itself without your explicit memory allocation 
    array.resize(length); 
} 

在第一種情況下定義例如一個指針一個地址到一個內存位置,它將容納數組,在第二種情況下,你聲明一個將爲你處理內存的對象(或多或少)。

同時看看矢量類:

Vector c++

一個初學者的建議件:努力學習和使用C++容器(從STL,BOOST),通常的實現是真的好,而且你不會冒險將自己踢在腳下(指針是「危險的」,但是當你學習語言時,你會發現這是非常有用和強大的)。

+0

我會接受,但我得到「請求失敗,你可以在3分鐘內接受答案」 –

+1

@SneakyTurtle沒關係;似乎有人真的生氣了,只是把我投給了......東西.... – asalic

0

我會建議使用std::vector而不是普通的C array,尤其是如果你來自javascript。

因此,您可以修改代碼:

class Name { 
    public: 
    Name(unsigned int); 
    unsigned int getArraySize(); 
    std::vector<unsigned int> getArray(); 

    private: 
    std::vector<unsigned int> _array; 
} 

(在C++中,它首先聲明公共成員(最常用的公共API),然後一類的私有成員通常成員變量的約定。已命名約定不像我以前_m_,來區分它們的人。)

unsigned int Name::getArraySize() { 
    return _array.size(); 
} 

std::vector<unsigned int> getArray() { 
    std::vector<unsigned int> outArray; 
    outArray = _array; 
    return outArray; 
} 

Name::Name(unsigned int len) 
: _array(len) 
{ 
} 

構造函數將創建len維數組,你可以看看可用的構造函數在http://en.cppreference.com/w/cpp/container/vector/vector
您可以稍後更改數組的長度,pushing元素轉換爲矢量,或resizing它。在使用[i]訪問元素時,請注意保留i < _array.size()

函數getArraySize返回數組中元素的數量。 std ::是標準模板庫的名稱空間,可用於所有C++編譯器。 size是STL中容器長度的命名(std :: vector)。

函數'getArray'應該返回一個數組的副本,我寫了一個prolissic版本,該函數的短版本是{ return _array; }