Passing a 2D vector to another class

Hi All,

I am trying to make a copy of a 2D vector which is private and located in class A to the class B. I am thinking a get function but I have an error in declaring the target vector for the "get" function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class A{
private:
vector<vector<int>> examVector;

public:
vector<vector<int>> getVector() {return examVector;}
};


class B{
int TotalTimeMod = st->getTotalTime();
int TotalDistMod = pm->get_total_dist();
// TotalTimeMod & TotalDistMod come from other classes.
std::vector<vector<double>> copyVector(TotalDistMod, std::vector<double>(TotalTimeMod));

copyVector = a->getVector();  // a is a pointer of the class A

};



I tried to use the i and j as the first and second element of the 2DVector to pass element by element, but it seems I need to declare the vector in class A at first and I can NOT!. However, since it might be slower maybe copying whole the vector is a better option.
Appreciate it if you let me know how to solve this problem.

Best, Reza
Last edited on
RezaAb wrote:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class A{
provate:
vector<vector<int>> 2DVector;

Public:
vector<vector<int>> getVector() {return 2DVector;}
};


class B{
int TotalTimeMod = st->getTotalTime();
int TotalDistMod = pm->get_total_dist();
// TotalTimeMod & TotalDistMod come from other classes.
std::vector<vector<double>> copyVector(TotalDistMod, std::vector<double>(TotalTimeMod));

copyVector = a->getVector();  // a is an object of the class A

}

I have no problem to admit I can’t understand your code.
Anyway:

a) provate: must become private:

b) vector<vector<int>> 2DVector;
A valid identifier must begin with a non-digit character

http://en.cppreference.com/w/cpp/language/identifiers

c) Public: must become public:

d)
1
2
3
class B{
    . . .
}

final semicolon missing.

e) std::vector<vector<double>> copyVector(TotalDistMod, std::vector<double>(TotalTimeMod));
Are you really trying to dynamically create a std::vector inside the parameters list?
A function declaration is a general pattern; every times you invoke the function you need to specify the different arguments you are going to use at that invocation - and they are supposed to exist already.

f) copyVector = a->getVector(); // a is an object of the class A

f.1) “copyVector” is a function, it can’t be invoked without parenthesis “copyVector()”.

f.2) Do you really want to assign a value to your function? You’d better reading @mbozzi explanation here:
http://www.cplusplus.com/forum/beginner/217298/
Or perhaps do you want to pass that value as an argument?
copyVector(a->getVector());
In this last case, it seems you are missing an argument (the first).

f.3) // a is an object of the class A
In your example “a” must be a pointer to an instance of A.

f.4) The only way to help you on your copyVector() function is reading its body. What about posting it on this forum?

g)
I tried to use the i and j as the first and second element of the 2DVector to pass element by element, but it seems I need to declare the vector in class A at first and I can NOT!

Declare or instance? If you haven't got an instance of your std::vector, what do you want to copy?

Can you please explain better or provide a compilable example of what you're trying to achieve?
Last edited on
Dear Enoizat,
Thank you for your reply. Since I posted in the car via my phone, I made some grammar mistakes. I edited the code.
for your comments:

e) Since I have my writer function in class B, I need to make a copy of the "examVector" 2D vector to pass the copied vector to the function.

f) I am not sure about this line, I have 13 2D similar vectors and I am looking for the best way to do this. Maybe the following code is not the correct way:
 
copyVector = a->getVector();


But I am looking for a way to make a copy of the vector.

g) I think the declaring vector's size is not the problem. I tried it but also got an error!

for the last part of your comment, As I said earlier, I need to make a copy of a 2D vector in a different class, plain and simple! Since would like to copy whole the vector after all of the calculations and just for writing the results, I will use this "getVector()" function for one time.

BTW, I tried following way too but got an error as well!
vector<vector<double>> getVector(int i, int j) { return examVector[i][j]; }
Here is the error message, maybe it helps to better guide me:
no suitable constructor exists to convert from "double" to "std::vector<double, std::allocator<double>>, std::allocator<std::vector<double, std::allocator<double>>>> "


Thanks
Last edited on

... trying to make a copy of a 2D vector which is private and located in class A to the class B.

you could try containment/composition i.e. declare an instance of A as a data-member of B:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# include <iostream>
# include <vector>

using TD_Vec = std::vector<std::vector<int>>;

class A
{
    private:
        TD_Vec examVector{2, std::vector<int>{0, 0}};
    public:
        const TD_Vec& getVector()const
        {
            return examVector;
        }

};

class B
{
    private:
        A m_a;
    public:
        const A& aOFb()const
        {
            return m_a;
        }

};

int main()
{
    B b{};
    auto the_A_of_b = b.aOFb().getVector();
    for (const auto& elem : the_A_of_b)
    {
        for (const auto& elem_inner : elem)
        {
            std::cout << elem_inner << " ";
        }
        std::cout << "\n";
    }
}
Topic archived. No new replies allowed.