2015-10-14 80 views
0

这里是我的代码无效使用非静态数据成员C++

的main.cpp

#include <iostream> 
#include "header.h" 
#include "source.cpp" 

using namespace std; 

int main() 
{ 
    cout << "Hello world!" << endl; 
    int testarray[]={1,3,5,7}; 
    mymatrix* first=new mymatrix(testarray,2,2); 
    return 0; 
} 

和header.h

using namespace std; 
#include <iostream> 
#include <string> 

class mymatrix{ 
public: 
    int i; 
    int j; 
    int marray[]; 

    mymatrix(int m[],int rows,int cols) : marray(m),i(rows),j(cols) 
    { 
     cout<<"this is for testings "; 
    } 
    mymatrix() 
    {}; 
    ~mymatrix(){ 
    // delete[] marray; 
    }; 


}; 

我得到这个错误:无效使用非静态的数据成员myMatrix的::我

做的是我想做的让我 矩阵类的对象,并传递一个数组

+0

是什么'MARRAY [] []'是什么意思? – ar2015

+0

在这行你得到这个错误? – deviantfan

+0

INT I,诠释J <<那行 – Mperera

回答

0

int marray[]; 

转换它

int *marray; 

此外,无论是采用C范例或使用C++一个但不是该混合物。

代替

mymatrix* first=new mymatrix(testarray,2,2); 

使用

mymatrix first(testarray,2,2); 

让编译器分配和释放内存,而不是你。

如果你不知道你用C++库的限制,考虑std::vector库来管理您的动态数组。

内存管理出物体的Instad,管理它的对象专门里面的构造函数和析构函数中。

+0

谢谢,但问题发生对于int i和诠释J。这建议是有益的,虽然 – Mperera

+0

哇我删除“:MARRAY(米),I(行),J(COLS) “部分和appiled这然后我工作,感谢很多 – Mperera

相关问题