Hi all,
I tried to use STL for a C++ class that can contain arrays of itself as, for
example, a directory tree. It is easy to implement this using normal pointers
but I tried to use the std::vector container and got compile errors.
The following is a code fragment:
#include <vector>
#include <string>
// forward declaration
class DirectoryEntryArray;
// describes a single directory entry: the name, the size, etc.. and
// the type of the entry: it could be a subdirectory
class DirectoryEntry
{
public:
std::string m_name;
int m_size;
std::string m_dateCreated;
int m_type; // 0=FILE, 1=LINK, 2=DIRECTORY, ...
// if m_type == DIRECTORY the entry contains an array of other entries
// we need a pointer because the array has an incomplete type
DirectoryEntryArray *m_array;
};
typedef std::vector<DirectoryEntry> DirectoryEntryArray;
// the Directory class defines the interface for navigating
// the directory and sub-dirs, the initial path, etc..
class Directory
{
public:
Directory( const std::string& path );
// ... the interface ...
protected:
DirectoryEntryArray m_array;
};
int main( int argc, char* argv[] )
{
Directory dir( "/home" );
return 0;
}
The code does not compile on my linux machine GCC 4.0.3. Here is the error:
test.cpp:27: error: conflicting declaration ‘typedef class
std::vector<DirectoryEntry, std::allocator<DirectoryEntry> >
DirectoryEntryArray’
test.cpp:10: error: ‘struct DirectoryEntryArray’ has a previous declaration as
‘struct DirectoryEntryArray’
Also, the code does not compile on win32 using Borland BCC 5.5:
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
test.cpp:
Error E2238 test.cpp 27: Multiple declaration for 'DirectoryEntryArray'
So I think the code is surely wrong but do not undestand why is it wrong.
Luciano