define a vector in a class? and put this class in another vector?
Aug 9, 2013 at 2:22pm UTC
First this is my code:
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>
#include <cstdlib>
using namespace std;
class pore_t {
int T[4];
};
class throat_t {
int P[2];
};
class cluster_t {
vector<pore_t> P;
vector<throat_t> T;
};
int main() {
vector<cluster_t> C;
cout<<"Input Cluster Numbers:" <<endl;
int clusterN;
cin>>clusterN;
for (int i=0; i<clusterN; i++) {
cluster_t currentC;
srand(i);
int dimension=rand()%10+1;
for (int j=0; j<dimension; j++) {
currentC.P.push_back(j);
currentC.T.push_back(j);
}
C.push_back(currentC);
}
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
the blacked content got problems: the error messages are the throat_t::P or throat_t::T is inaccessible. How to fix this? Thanks.
Aug 9, 2013 at 2:27pm UTC
classes default to private members. If you want access to them from outside the class you need to make them public:
1 2 3 4 5 6 7
class pore_t{
{
public : // <- need this
int T[4];
};
// repeat for your other classes
Also... your P and T vectors are of pores and throats... not of
int
s. 'j' is an int... so you can't push_back j into those vectors. You need to push_back a pore_t or a throat_t.
Last edited on Aug 9, 2013 at 2:27pm UTC
Aug 9, 2013 at 2:30pm UTC
You haven't declared your access specifyors to be able to access your T and P class members from outside you need to declare them as public:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class pore_t
{
public :
int T[4];
};
class throat_t
{
public :
int P[2];
};
class cluster_t
{
public :
vector<pore_t> P;
vector<throat_t> T;
};
Aug 9, 2013 at 2:41pm UTC
Thanks!!!
Topic archived. No new replies allowed.