mazlor
mazlor

Reputation: 1895

matrix in a class in C++

the following code is for three classes , class One, class Two, class Three.

class Three takes tow vectors, the first vector contains instances of One , the second vector contains instances of Two.

I want to get a 2D matrix via a method in Three , this matrix will have two equal indices each one is the size of the vector of One instances.

I don't know where to declare this matrix , and how to initialize it.

i will present a code is working fine before i declare the matrix , then i will present one example of my many tries which is not working and producing error messages.

here is the code before declaring the matrix(it works fine)

#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;
}

now i declared a method to produce a matrix in Three , the method's name is get_Mat() here is the code :

#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;
}

I will be very thankful if you can help me to find a way to produce this matrix via a method in class Three.

Thanks.

Upvotes: 0

Views: 661

Answers (1)

Patrik
Patrik

Reputation: 2691

get_Mat returns an integer, not a matrix. It is better to use vector<vector<unsigned int> >, that will avoid a lot of troubles later on.

Or have a look here (c++):

Return a 2d array from a function

or here (C):

Return a 2d array from a function

Upvotes: 2

Related Questions