简体   繁体   English

使用C ++的类中的2D数组

[英]2D array in a class using c++

I wrote a code to present a class Third takes instances of other two classes One , and Two respectively , everything was working fine until i added a matrix Mat , and the method get_Mat in the third class , in the code it has the name Third, this code doesn't produce any error message , but when execute it does until the line before return 0 in main , then it terminate as something wrong was encountered by the compiler and need to be closed , i wish that you can help me find the problem. 我编写了一个代码来表示类Third,它分别接受了其他两个类One和Two的实例,直到我在第三个类中添加了矩阵Mat和方法get_Mat为止,一切正常,在代码中它的名称为Third,该代码不会产生任何错误消息,但是执行时会一直执行到main返回0之前的那一行,然后由于编译器遇到错误并需要将其关闭而终止,希望您能帮助我找到问题。

Thanks. 谢谢。

#include<iostream>
#include<vector>
#include <stdlib.h>   
using namespace std;

class One        // this the first class
{
 private:
     unsigned int id;                                 
public:   
    unsigned int get_id(){return id;};   
    void set_id(unsigned int value) {id = value;};
    One(unsigned int init_val = 0): id(init_val) {};   // constructor
    ~One() {};                                         // destructor
};
////////////////////////////////////////////////////////////////////
class Two        // the second class
{
  private:
    One first_one;                 
    One second_one;                
    unsigned int rank;                      
  public:   
    unsigned int get_rank() {return rank;};
    void set_rank(unsigned int value) {rank = value;};
    unsigned int get_One_1(){return first_one.get_id();};
    unsigned int get_One_2(){return second_one.get_id();};

    Two(const One& One_1 = 0, const One& One_2 =0 , unsigned int init_rank = 0)
    : first_one(One_1), second_one(One_2), rank(init_rank)
     {
     }  

    ~Two() {} ; // destructor

};
///////////////////////////////////////////////////////////// 
class Three     // the third class  
{
private:
     std::vector<One>   ones;
     std::vector<Two>   twos;    
     vector<vector<unsigned int> > Mat;

public:
     Three(vector<One>& one_vector, vector<Two>& two_vector)
    : ones(one_vector), twos(two_vector)
     { 
       for(unsigned int i = 0; i < ones.size(); ++i)
            for(unsigned int j = 0; j < ones.size(); ++j)
                Mat[i][j] = 1;
     }

     ~Three() {};

     vector<One> get_ones(){return ones;};
     vector<Two> get_twos(){return twos;};
     unsigned int get_Mat(unsigned int i, unsigned int j) { return Mat[i][j];};
     void set_ones(vector<One> vector_1_value) {ones = vector_1_value;};
     void set_twos(vector<Two> vector_2_value) {twos = vector_2_value;};


};
///////////////////////////////////////////////////////////////////////
int main()
{
cout<< "Hello, This is a draft for classes"<< endl;
vector<One> elements(5);
cout<<elements[1].get_id()<<endl;

vector<Two> members(10);
cout<<members[8].get_One_1()<<endl;

Three item(elements, members);
cout<<item.get_ones()[3].get_id() << endl;  

cout << item.get_Mat(4, 2) << endl;
return 0;
}

First, when you construct your object of class Three here: 首先,当您在此处构造类Three的对象时:

 Three item(elements, members);

its Mat member is a vector<vector<unsigned int> > of size zero. 它的Mat成员是大小为零的vector<vector<unsigned int> > It is pure coincidence that the constructor does not crash right away. 纯粹的巧合是,构造函数不会立即崩溃。 For example if you need a matrix of size n x m , you would have to do 例如,如果您需要大小为n x m的矩阵,则必须

 Mat.resize(n);
 for(unsigned int i =0;i<n;++i)
      Mat[i].resize(m);

before you can safely use expressions like Mat[i][j] . 在可以安全地使用Mat[i][j]类的表达式之前。

Second, in your constructor of Three : 其次,在您的Three的构造函数中:

   for(unsigned int i = 0; i < ones.size(); ++i)
        for(unsigned int j = 0; j < ones.size(); ++j)
            Mat[i][j] = 1;

is it intended that you don't use twos.size() in one of the loops? 是否打算在其中一个循环中不使用twos.size()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM