2012-03-28 91 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