An object containing lists recursively

For practice, I am trying to make a recursive directory parser.

To express the syntax, I want to also produce the resulting result, for example:

1 class CDirectory 2 { 3 private: 4 std::string name; 5 std::vector<CDirectory> subDirectories 6 public: 7 //Various things, constructors etc. go here 8 } 

However, here I see that line 5 is not supported by the behavior - "The C ++ (2003) standard clearly states that instantiating a standard container with an incomplete type causes undefined-behavior."

What should I do? Is there no way to make an object containing a list of similar objects? If nothing else, I know that it is by no means illegal to create a vector of vectors, so an object that contains itself.

+6
source share
2 answers

Make Pointer Vector

 std::vector<CDirectory*> subDirectories; 
0
source

Boost has containers that support incomplete types . You can use one of them.

 #include <boost/container/vector.hpp> class CDirectory { private: std::string name; boost::container::vector<CDirectory> subDirectories public: //Various things, constructors etc. go here }; 
+1
source

Source: https://habr.com/ru/post/979696/


All Articles