0

以下代码适用于三个类,一类,二类,三类。

三类接受两个向量,第一个向量包含 One 的实例,第二个向量包含 Two 的实例。

我想通过 Three 中的方法获得一个二维矩阵,这个矩阵将有两个相等的索引,每个索引都是 One 实例向量的大小。

我不知道在哪里声明这个矩阵,以及如何初始化它。

在我声明矩阵之前,我将展示一个代码工作正常,然后我将展示我的许多尝试中的一个示例,该示例不工作并产生错误消息。

这是声明矩阵之前的代码(它工作正常)

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

const unsigned int N = 5;

class One
{
 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
{
  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  
{
private:
     std::vector<One>   ones;
     std::vector<Two>   twos;    


public:
     Three(vector<One>& one_vector, vector<Two>& two_vector)
    : ones(one_vector), twos(two_vector)
     {
     }

     ~Three() {};

     vector<One> get_ones(){return ones;};
     vector<Two> get_twos(){return twos;};

     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;  

return 0;
}

现在我声明了一个在 Three 中生成矩阵的方法,该方法的名称是 get_Mat() 这是代码:

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

const unsigned int N = 5;

class One
{
 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
{
  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  
{
private:
     std::vector<One>   ones;
     std::vector<Two>   twos;    


public:
     Three(vector<One>& one_vector, vector<Two>& two_vector)
    : ones(one_vector), twos(two_vector)
     {
     }

     ~Three() {};

     vector<One> get_ones(){return ones;};
     vector<Two> get_twos(){return twos;};

     void set_ones(vector<One> vector_1_value) {ones = vector_1_value;};
     void set_twos(vector<Two> vector_2_value) {twos = vector_2_value;};

     unsigned int get_Mat() {
                         unsigned int mat[ones.size()][ones.size()];
                         for(unsigned int i = 0; i < ones.size(); ++i)
                              for(unsigned int j = 0; j < ones.size(); ++j)
                                    mat[i][j] = 1;
                          return mat;}          

};



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;  

return 0;
}

如果您能帮助我找到一种通过第三类中的方法生成此矩阵的方法,我将非常感激。

谢谢。

4

1 回答 1

2

get_Mat返回一个整数,而不是矩阵。最好用vector<vector<unsigned int> >,以后省去很多麻烦。

或者看看这里(c++):

从函数返回二维数组

或在这里(C):

从函数返回二维数组

于 2013-10-30T14:28:04.453 回答