2012-03-28 92 views
0

我正在爲我的作業工作,因爲一些部分不是很好解釋我有一些問題,有我的結構和我的構造函數,它必須是動態的,但我得到了下游錯誤。一些幫助真的很感謝你。 .H:C++ struct構造函數錯誤

const int days=31; 
const int exp=6; 

struct Array{ 
int days; 
int exp; 
int **M; 
}; 

的.cpp:

void constr(Array &loc){ 
//Construct of 31*6 Matrix, were 31 nr. of days and 6 specific types: 
//0-HouseKeeping, 1-Food, 2-Transport, 3-Clothing, 4-TelNet, 5-others 
loc.days = days; 
loc.exp = exp; 
loc.M=malloc(loc.days*sizeof(int*)); 
for(int i=0; i<loc.days;i++){ 
    loc.M[i] = malloc(loc.exp*sizeof(int)); 
    for (int j = 0; j< loc.exp; j++){ 
     loc.M[i][j] = 0; 
    } 
} 
} 

錯誤:

..\src\structs.cpp: In function 'void constr(Array&)': 
..\src\structs.cpp:7:36: error: invalid conversion from 'void*' to 'int**' [-fpermissive] 
..\src\structs.cpp:9:40: error: invalid conversion from 'void*' to 'int*' [-fpermissive] 
+1

malloc返回void *因此顯式必須將其轉換爲int *在您的情況。但是,爲什麼不使用new而不是malloc。如果你被告知使用malloc,那麼很好。 – Jagannath 2012-03-28 09:52:53

+0

typecast malloc – Jeeva 2012-03-28 09:53:59

+0

'void constr'實際上是一個自由函數。一個合適的C++構造函數將是'Array'的成員。 – Stephan 2012-03-28 09:54:34

回答

2

由於這是C++:

loc.M = new int*[loc.days]; 
for(int i=0; i<loc.days;i++){ 
    loc.M[i] = new int[loc.exp]; 
    for (int j = 0; j< loc.exp; j++){ 
     loc.M[i][j] = 0; 
    } 
} 
+0

謝謝,我會馬上試一試。 – 2012-03-28 10:00:40

+0

.. \ src \ /structs.cpp:7:36:錯誤:從'void *'無效轉換爲'int **'[-fpermissive] .. \ src \ /structs.cpp:9:40:錯誤:無效轉換從'void *'到'int *'[-fpermissive] 仍然有這種類型的錯誤:< – 2012-03-28 10:10:50

+0

@BogdanMaier你做錯了什麼,然後,它應該編譯:http://ideone.com/X7Wjq – 2012-03-28 10:42:47

1
loc.M = (int**)malloc(loc.days*sizeof(int*)); 
loc.M[i] = (int*)malloc(loc.exp*sizeof(int)); 
3

由於您在評論中要求提供C++構造函數......請參閱下面的代碼。我也用C++向量替換了你的二維C風格的數組。我添加代碼註釋到相關行:

Array.h:

#pragma once 

#include <vector> 

struct Array 
{ 
    // this is a c++ constructor declaration 
    Array(int daysParam, int expParam); 

    int days; 
    int exp; 

    // use a vector of vectors instead allocating with new or malloc 
    // it is easier to initialize and the compiler will clean it up for you 
    std::vector<std::vector<int> > M; 
}; 

Array.cpp:

#include "Array.h" 

// Array constructor definition with initializer list 
// all data members are initialized here by invoking their constructor 
Array::Array(int daysParam, int expParam) 
    : days(daysParam), 
     exp(expParam), 
     M(daysParam, std::vector<int>(expParam, 0)) 
{ 
} 

示例的Array使用(Program.cpp):

#include "Array.h" 

int main() 
{ 
    // create a new Array, using the c++ constructor 
    Array myArray(31, 6); 

    // access elements in the 2-dimensional array 
    int singleValue = myArray.M[15][3]; 

    return 0; 
} 

我強烈建議您閱讀book about C++

+1

這是真正的C++方法。我仍然想知道他是否需要了解指針,他可能需要指針解決方案。 – Sambatyon 2012-03-28 10:39:44

0

請停止使用std :: vector>或更糟的T tab [] []來表示2D數組。您應該使用一維數組來存儲數據,一個索引數組來存儲行指針。這樣,你的數據保持連續,你仍然可以有一個很好的語法。

template<typename T> 


class Array2D 



{ 
    std::vector<T> m_data; 
    std::vector<T*> m_ptr; 
    size_t m_iWidth; 
    size_t m_iHeight; 

    void Link(void) 
    { 
     for (unsigned int j = 0; j < m_iHeight; ++j) 
     m_ptr[j] = &m_data[j * m_iWidth]; 
    } 


    public: 
    Array2D(void) 
    { 
    }; 
    Array2D(const size_t i_width, const size_t i_height) : 
     m_iWidth(i_width), 
     m_iHeight(i_height), 
     m_data(i_width * i_height), 
     m_ptr(i_height) 
    { 
     Link(); 
    } 


    void Resize(const size_t niou_width, const size_t niou_height) 
    { 
     if (m_iWidth == niou_width && m_iHeight == niou_height) 
     return; 

     m_iWidth = niou_width; 
     m_iHeight = niou_height; 

     m_data.resize(niou_height * niou_width); 
     m_ptr.resize(niou_height); 
     Link(); 
    } 


    typename std::vector<T>::iterator begin(void) 
    { 
     return m_data.begin(); 
    } 


    typename std::vector<T>::iterator end(void) 
    { 
     return m_data.end(); 
    } 


    void assign(T value) 
    { 
     m_data.assign(m_iWidth * m_iHeight, value); 
    } 


    Array2D(const Array2D& a) : 
     m_iWidth(a.m_iWidth), 
     m_iHeight(a.m_iHeight), 
     m_data(a.m_data) 
    { 
     m_ptr.resize(m_iHeight); 
     Link(); 
    } 


    Array2D& operator=(const Array2D a) 
    { 
     swap(*this, a); 
     return *this; 
    } 

    template <typename U> 
    friend void swap(Array2D<U>& first, Array2D<U>& second) 
    { 
     using std::swap; 
     swap(first.m_iHeight, second.m_iHeight); 
     swap(first.m_iWidth, second.m_iWidth); 
     swap(first.m_data, second.m_data); 
     swap(first.m_ptr, second.m_ptr); 
    } 

    ~Array2D() 
    { 
    }; 

    T* operator[](const size_t ligne) 
    { 
     return m_ptr[ligne]; 
    }; 
    const T* operator[](const size_t ligne) const 
    { 
     return m_ptr[ligne]; 
    }; 

    T& operator()(const size_t col, const size_t lig) 
    { 
     return m_ptr[lig][col]; 
    }; 
    const T& operator()(const size_t col, const size_t lig) const 
    { 
     return m_ptr[lig][col]; 
    }; 
+0

我不允許使用vector:< – 2012-03-28 11:47:37