problem with vector of other classes

hi,

please see with me this pb

1
2
3
4
5
6
7
8
9
class XXX{
......
};
class YYY{
public:
....
private:
vector<XXX*> composant_xxx;  // this ligne generate an error
}

the error is

1
2
 error: ‘XXX’ was not declared in this scope
There isn't anything wrong with that. However the error you're getting hints that you're declaring YYY above XXX. Something like:

1
2
3
4
5
6
class YYY{
vector<XXX*> v;
};

class XXX
{};


Which wouldn't work. Double-check the order in which files are #included in the cpp file(s) that's throwing the error.
i have done that but the same error!!!!
Then your code must not be arranged the way you think it is. The example you pasted works -- I cannot tell you what's wrong without more information. The following demo program compiles just fine (compile it to see for yourself):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <vector>
using namespace std;

class XXX
{
};

class YYY
{
  vector<XXX*> v;
};

int main()
{
  YYY y;
  return 0;
}


Other possible solutions are to forward declare XXX before you declare YYY:

1
2
3
4
5
6
class XXX;  // forward declaration

class YYY
{
vector<XXX*> v;
};


or forcibly #include "xxx.h" in "yyy.h" (be sure you protect your #include with typedef)

1
2
3
4
5
6
7
8
9
/// -- in xxx.h:
#ifndef __XXX_H_INCLUDED__
#define __XXX_H_INCLUDED__

class XXX
{
};

#endif 

1
2
3
4
5
6
7
8
9
10
11
12
/// -- in yyy.h:
#ifndef __YYY_H_INCLUDED__
#define __YYY_H_INCLUDED__

#include "xxx.h"

class YYY
{
  vector<XXX*> v;
};

#endif 
Topic archived. No new replies allowed.