> It looks like you are creating a root for every block. This is not a good idea. You need one root for the entire tree. In your example the root should be for E:\.
But I've got this line:
Code:
//Directory *root = new Directory(myRoot); //it creates root
Directory root(myRoot, whereInFile); //it creates object root
in this block of code:
Code:
//-----------------------------------------------------------------------
//first iteration - it checks version of dir/s and reads name of main dir
//-----------------------------------------------------------------------
I'm not so sure what to write here:
Code:
case 4:
if (line[0] != ' ')
{ //case 4a - main body of the block
actualName = line.substr(36); //name of new file or directory
if (line[21] == '<') //it is directory
{
// ????
}
}
At first I thought about writing something like this:
Code:
//Directory *sub1 = new Directory("sub1", whereInFile);
//root.children.push_back(sub1); (or "root.children.push_back(sub1, whereInFile);" ?)
and
Code:
//Directory *sub11 = new Directory("sub1.1");
//sub1->children.push_back(sub11);
and I think that's not good way to do it.
It should be better to create it in two stages. 1) create object, 2) insert object into tree (in right place). So I created "Directory root(myRoot, whereInFile);" in line 104 and now I can create other object in line 150:
Code:
Directory new_directory(actualName, whereInFile); //this is the only one new line added to the code from message #30
ActualName is created in line 147:
Code:
actualName = line.substr(36); //name of new file or directory
and whereInFile should work as well. It is declared in line 64:
Code:
long int whereInFile = 0; //what number of line in the file is the actual one
and increased every time in line 183
Code:
//every iteration it should increase whereInFile by one
whereInFile++;
At first time it should be actualName=="katalog 1" and whereInFile==1.
And my problem is that this line:
Code:
Directory new_directory(actualName, whereInFile);
will be creating new_directory every time. I need NEW, OTHER object, not the same object called new_directory every time.
And the other problem is with inserting object into tree. I've got in line 29:
Code:
vector<Directory*> children; //children directories
but it is inside the class. I guess I need something outside the class so that I can store table of objects or something like this.
Added: 15 Dec 2008
I see it may be too much to analyse my previous post. So here is my problem written with no unnecessary code. It looks like I've got problem with the same thing as at the beginning, about one month ago - with this tree structure (creating new branches).
Code:
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
class Directory
{
private:
Directory::Directory(const Directory& dir) {}
void Directory::operator=(const Directory& dir) {}
public:
string name; //name of the directory
vector<Directory*> children; //children directories
long int lineInFile; //What is the number of line in the file
//where list of files in this directory begins
//(don't mistake it with whereInFile)
Directory(string name_ = "No Name", long int lineInFile_ = -1) :
name(name_), lineInFile(lineInFile_) { }; //default constructor
//lineInFile == -1 means error
~Directory()
{
//delete children;
for (int i = 0; i<children.size(); i++)
{ delete children[i]; }
};
};
int nesting = 0;
void print(Directory *dir) { //my print function needs to take a pointer...
for (int i = 0; i < nesting; ++i) cout << ' ';
cout << dir->name << endl; //...so I've got this...
++nesting;
for_each(dir->children.begin(), dir->children.end(), print); //...and this.
--nesting;
}
int main()
{
//myRoot
//---firstSub
//------firstSubA
//------firstSubB
//---secondSub
//------secondSubA
//---thirdSub
Directory root("myRoot", 0); //it creates object root
string names = ("firstSub", "firstSubA", "firstSubB", "secondSub", "secondSubB", "thirdSub"); //examplary data
int numbers = (10, 14, 22, 29, 36, 46); //examplary data
for (int i=0; i<6; i++)
{
//what kind of recursive function should it be ??
//Directory branch(names[i], numbers[i]);
}
system("pause"); return 0;
}
Regards!