我编写了一个代码来呈现一个类 Third 分别获取其他两个类 One 和 Two 的实例,一切正常,直到我添加了一个矩阵 Mat 和第三类中的 get_Mat 方法,在代码中它的名称为 Third,这段代码不会产生任何错误信息,但是当执行它直到main中return 0之前的那一行,然后它终止,因为编译器遇到错误需要关闭,我希望你能帮我找到问题。
谢谢。
#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;
}