vector of object, object including vectors again?

I want to define an object, in this object I need to define two vectors which I don't know the size in advance. Can I do this in this way?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class cluster_type() {
public:
    vector<point_type> point;
    vector<line_type> line;
    /*
      point_type and line_type are defined, their construct function are also 
      defined
    */

    cluster_type() {} 
    /*
      I actually don't know how to construct it, when I call it I want an 
      cluster object created, in which there are an empty point_type vector and 
      an empty line_type vector, please help me out
    */
};


the other thing is how can I use such a class in an vector: for example how to:

1
2
3
4
5
6
7
8
9
10
//How to create a vector of this object?
vector<cluster_type> C;
C.push_back(cluster_type());

//How to use each one's member?
C[0].point.push_back(point_type()); //or
(C[0].point).push_back(point_type());

cout<<C[0].point[0];


If you look at the code, you know what I want to do, do you think I am doing it in the right way? Thanks alot!
I see nothing terribly wrong with what you're doing, are you having compile errors or other problems?
Thanks, I didn't compile at all, because I didn't know how to define the constructor

cluster_type() {}
In this case you don't have to define the constructor, the only error I see is that the class name on line 1 has parens ;)
thanks, let me try a simple one and I will get back the compile message soon. Thanks.
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
#include <iostream>
#include <vector>

using namespace std;

class point_t {
public:
	double x, y;
	point_t() {
		x=0;
		y=1;
	}
};

class cluster_t {
public:
	vector<point_t> P;
};



int main() {
	vector<cluster_t> C;

	C.push_back(cluster_t());
	C[0].P.push_back(point_t());
	C[0].P.push_back(point_t());

	cout<<C[0].P[0].x<<C[0].P[0].y<<endl
	    <<C[0].P[1].x<<C[0].P[1].y<<endl;

	return 0;
}


Thanks, it really works!
Topic archived. No new replies allowed.