C++ constructor

I have to implement a constructor for a linked list directory(a directory implementation backed by a linked list) with private members
1)name(//the file name or the directory name
//if this is the root (i.e. root directory), then the name will be exactly \
(a single slash)
//notice that in your code you usually need to write \\ instead of \
//for this assignment, except for the root's, you can assume the name has
only uppercase alphabets, lowercase alphabets, and numbers)
2)parent(//pointer to the parent FSObject (a file system object)
//in this assignment, the parent is either SADirectory* or LLDirectory*
//or NULL if this is the root, because root is the only top-level directory)

.cpp implementation:

LLDirectory::LLDirectory(string name="", FSObject*parent=NULL): FSObject(name,parent){}

I am getting an error in that statement and I am not sure what the problem is. I would be grateful if anyone can solve my problem. Thank you!
Last edited on
One line of code and "an error" doesn't give enough to go on.

Especially when it's easy enough to make it work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;

class FSObject {
    string mName;
    FSObject *mParent;
public:
    FSObject(string name,FSObject *parent) : mName(name), mParent(parent)
    {}
};

class LLDirectory : public FSObject {
public:
    LLDirectory(string name="", FSObject*parent=NULL): FSObject(name,parent)
    {}
};

int main ( ) {
}

One can only make wild guesses as to which of the 000's of things you could have done wrong.


http://sscce.org/
This is the mplementation of the header file
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#ifndef LLDIRECTORY_H
#define LLDIRECTORY_H

#include "Directory.h"

//Linked-list directory
//it is a directory that is backed by a linked list
//you should read the Directory.h first to see what each member function should do
class LLDirectory : public Directory
{
public:
    //constructor
    //hint: remember to initialize all data members properly!
    LLDirectory(string name, FSObject* parent);

    //destructor
    //delete the whole linked list
    //avoid memory leak
    virtual ~LLDirectory();

    //add a child
    //(please read Directory.h first)
    //for LLDirectory, when you add a child, always maintain the ascending lexicographical order of file/directory names
    //i.e. find a proper place to insert the new linked list node
    //we do not distinguish directories from files when we order them
    //e.g., if there are 3 files of names "a", "c", "A" and there are 3 directories of names "b", "e", "Z"
    //then they should be stored in the linked list in the following order
    //A [Z] a [b] c [e]
    //hint: for lexicographical ordering, please refer to the webpage for tips and info
    //hint: you need to create either a new LLDirectory or new File object here for adding a child
    virtual bool addChild(bool isDir, string name);

    //remove a child
    //avoid memory leak
    virtual bool removeChild(string name);

    //return a child by name
    virtual FSObject* getChild(string name);

    //return a child by index
    //i.e. return the data in the i-th node in the linked list
    //0-th (first) node is the head node
    virtual FSObject* getChild(int i);

    //return the string that represents the list of children in this directory
    //for LLDirectory, the order of the items is simply the linked list node order
    //i.e., data_in_head_node data_in_second_node data_in_third_node ...
    //if you refer to the example given in addChild, then the return value of this function would be simply
    //"A [Z] a [b] c [e]"
    //as you are supposed to make sure the children are in sorted order when we add a child in addChild,
    //no sorting/reordering needs to be done in this function
    //simply return "<empty>" if the directory is empty (i.e., has no child)
    virtual string list();

    //return the number of children
    //so count the number of nodes you have in the linked list
    virtual int getCount();

private:
    struct LLNode
    {
        LLNode* next; //the next pointer
        FSObject* data; //the data pointer that points to the child, i.e., either a File or a LLDirectory
    }; //this is the linked list node structure you should use for the linked list

    LLNode* head; //the pointer that points to the head node of the linked list of children
    //it should be NULL initially
    //notice that the linked list should have exactly N nodes
    //(including the head node) when there are N items (children)
};

#endif  
Last edited on
This is the implementation of the Directory file from which lldirectory inherits properties. It is completed already.

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef DIRECTORY_H
#define DIRECTORY_H

#include "FSObject.h"
#include "File.h"
#include <typeinfo>
#include <iostream>
using namespace std;

//a directory in the file system
//it is an ABC
//all child (file/directory) names are unique
class Directory : public FSObject
{
public:
    //constructor
    //already completed
    Directory(string name, FSObject* parent) : FSObject(name, parent) {};

    //destructor
    //already completed
    virtual ~Directory() {};

    //add a child
    //add a new directory, with the given name, under this directory if isDir is true
    //add a new file, with the given name, under this directory if isDir is false
    //the new file's content should be simply empty, i.e., ""
    //to make sure the child name is unique 
    //you should first check to see if an existing child already has the same name
    //if yes, simply do nothing and return false 
    //if no, proceed to add the child, and return true
    virtual bool addChild(bool isDir, string name) = 0;

    //remove a child (can be either a file or directory) from this directory
    //if no child has the given name, simply do nothing and return false
    //otherwise, proceed with the removal and return true
    virtual bool removeChild(string name) = 0;

    //return the child of the given name
    //return NULL if no child has the given name
    virtual FSObject* getChild(string name) = 0;

    //return the child of the given index
    //return NULL if index is out-of-range
    //if there are n children, then a valid index is in [0, n-1]
    virtual FSObject* getChild(int i) = 0;
    
    //return the string that represents the list of children in this directory
    //the children must be listed in the order specified by the actual directory type 
    //(for that please see either LLDirectory.h or SADirectory.h)
    //in the list, the directory names must be surrounded by a pair of square brackets, i.e., []
    //on the other hand, the file names do not have any surrounding brackets
    //e.g. if there are 3 directories named "a", "b", and "c"
    //and if there are 3 files named "f1", "f2", and "f3"
    //list() should return "[a] [b] [c] f1 f2 f3"
    //(assume this order; the actual order will depend on the actual Directory type)
    //notice that there is no extra spaces in the string at the beginning or at the end
    //and that there is exactly one single space between the items
    //see the sample output for more examples
    virtual string list() = 0;

    //return the number of children this directory has
    virtual int getCount() = 0;
};

#endif  
Last edited on
This is the implementation of the FSObject header file from which the directory header file inherits properties.

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
43
44
45
46
47
48
49
#ifndef FSOBJECT_H
#define FSOBJECT_H

#include <string>
using namespace std;

//File System Object
class FSObject
{
private:
    string name; //name
    //the file name or the directory name
    //if this is the root (i.e. root directory), then the name will be exactly \ (a single slash)
    //notice that in your code you usually need to write \\ instead of \
    //for this assignment, except for the root's, you can assume the name has only uppercase alphabets, lowercase alphabets, and numbers

    FSObject* parent; //pointer to the parent FSObject
    //in this assignment, the parent is either SADirectory* or LLDirectory*
    //or NULL if this is the root, because root is the only top-level directory

public:
    //default constructor
    //already completed
    FSObject() : name(""), parent(NULL) { } ;

    //constructor
    //already completed
    FSObject(string name, FSObject* parent) : name(name), parent(parent) { } ;

    //destructor
    //it is forced to be a pure virtual function so that FSObject becomes an ABC
    virtual ~FSObject() = 0;

    //return the name
    //already completed
    string getName() { return name; }

    //return the parent
    //already completed
    FSObject* getParent() { return parent; };

    //return the file system path of this object
    //you need to implement it in the cpp file
    string getPath();
};



#endif  
Last edited on
This is the implementation of the file header file.
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
#ifndef FILE_H
#define FILE_H

#include "FSObject.h"

//a file in the file system
//it can be used to store some string content
class File : public FSObject
{
private:
    string content; //the string content

public:
    //constructor
    File(string name = "", FSObject* parent = NULL) : FSObject(name, parent) {}

    //return the content
    //already completed
    string getContent() { return content; }

    //set the content
    //already completed
    void setContent(string content) { this->content = content; }

    //note: there is no File.cpp file - the class is already completed
};

#endif  
Last edited on
The main program in two replies,
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include "System.h"
#include "SADirectory.h"
#include "LLDirectory.h"

using namespace std;

int main()
{
    cout << boolalpha;

    {
        cout << "Testing SADirectory below..." << endl << endl;

        cout << "================= Test 1 =================" << endl;
        Directory* r = new SADirectory("\\", NULL);
        cout << r->addChild(true, "a") << endl;
        cout << r->addChild(true, "b") << endl;
        cout << r->addChild(false, "f1") << endl;
        cout << r->addChild(false, "f2") << endl;
        cout << r->addChild(false, "f3") << endl;
        cout << r->addChild(true, "c") << endl;
        cout << r->addChild(true, "c") << endl;
        string s = r->list();
        cout << r->list() << endl;
        if(s != "[a] [b] f1 f2 f3 [c]")
            cout << "Format/output is incorrect! Please check carefully. \nYour output= <" << s << "> \nExpected=    <" << "[a] [b] f1 f2 f3 [c]" << ">" << endl;

        cout << "================= Test 2 =================" << endl;
        cout << r->removeChild("b") << endl;
        cout << r->removeChild("nope") << endl;
        cout << r->list() << endl;

        cout << "================= Test 3 =================" << endl;
        cout << r->getPath() << endl;

        cout << "================= Test 4 =================" << endl;
        cout << (r->getChild("nope")==NULL?"NULL":r->getChild("nope")->getName()) << endl;
        cout << (r->getChild("c")==NULL?"NULL":r->getChild("c")->getName()) << endl;

        cout << "================= Test 5 =================" << endl;
        Directory* c = dynamic_cast<Directory*>(r->getChild("c"));
        cout << c->addChild(true, "ca") << endl;
        cout << c->addChild(true, "cn") << endl;
        cout << c->addChild(false, "cf1") << endl;
        cout << c->list() << endl;

        cout << "================= Test 6 =================" << endl;
        cout << c->getPath() << endl;
        cout << c->getChild("cn")->getPath() << endl;
        cout << c->getChild("cf1")->getPath() << endl;

        cout << "================= Test 7 =================" << endl;
        delete r;
        cout << "SADirectory deleted. I shall make sure there is no memory leak." << endl;

        cout << "================= Test 8 =================" << endl;
        System<SADirectory>* system = new System<SADirectory>;
        cout << system->isValidPath("") << endl;
        cout << system->isValidPath("c") << endl;
        cout << system->isValidPath("\\") << endl;
        cout << system->isValidPath("\\c") << endl;
        cout << system->isValidPath("\\c\\cn") << endl;
        cout << system->isValidPath("\\c\\nope") << endl;

        cout << "================= Test 9 =================" << endl;
        cout << system->addDir("\\", "a") << endl;
        cout << system->addDir("\\", "b") << endl;
        cout << system->addFile("\\", "f1", "hi") << endl;
        cout << system->addFile("\\", "f2", "hello") << endl;
        cout << system->addFile("\\", "f3", "howdy") << endl;
        cout << system->addDir("\\", "c") << endl;
        cout << system->addDir("\\", "c") << endl;
        cout << "In \\ : " << system->list("\\") << endl;

        cout << "================= Test 10 =================" << endl;
        cout << system->del("\\", "b") << endl;
        cout << system->del("\\", "nope") << endl;
        cout << "In \\ : " << system->list("\\") << endl;

        cout << "================= Test 11 =================" << endl;
        cout << (system->getDirectory("\\")==NULL?"NULL":"\\") << endl;
        cout << (system->getDirectory("\\c")==NULL?"NULL":"c") << endl;
        cout << (system->getDirectory("\\nope")==NULL?"NULL":"nope") << endl;

        cout << "================= Test 12 =================" << endl;
        cout << system->getDirectory("\\")->getPath() << endl;
        cout << system->getDirectory("\\c")->getPath() << endl;

        cout << "================= Test 13 =================" << endl;
        cout << system->addDir("\\c", "ca") << endl;
        cout << system->addDir("\\c", "cn") << endl;
        cout << system->addFile("\\c", "cf1", "yo") << endl;
        cout << "In \\c : " << system->list("\\c") << endl;
        cout << (system->getFile("\\c", "cfnope")?"nope":"NULL") << endl;
        cout << system->getFile("\\c", "cf1")->getContent() << endl;
        system->getFile("\\c", "cf1")->setContent("yoooooooo");
        cout << system->getFile("\\c", "cf1")->getContent() << endl;

        cout << "================= Test 14 =================" << endl;
        cout << system->getDirectory("\\c\\cn")->getPath() << endl;
        cout << system->getFile("\\c", "cf1")->getPath() << endl;

        cout << "================= Test 15 =================" << endl;
        cout << "In \\ : " << system->list("\\") << endl;
        cout << "In \\a : " << system->list("\\a") << endl;
        cout << "In \\b : " << system->list("\\b") << endl;
        cout << "In \\c : " << system->list("\\c") << endl;
        cout << "In \\c\\cn : " << system->list("\\c\\cn") << endl;
        cout << "Move result = " << system->move("\\","f2","\\c","f2") << endl;
        cout << "In \\ : " << system->list("\\") << endl;
        cout << "In \\a : " << system->list("\\a") << endl;
        cout << "In \\b : " << system->list("\\b") << endl;
        cout << "In \\c : " << system->list("\\c") << endl;
        cout << "In \\c\\cn : " << system->list("\\c\\cn") << endl;

        cout << "================= Test 16 =================" << endl;
        cout << system->addFile("\\c\\cn","cnf","so deep!") << endl;
        cout << "In \\ : " << system->list("\\") << endl;
        cout << "In \\a : " << system->list("\\a") << endl;
        cout << "In \\b : " << system->list("\\b") << endl;
        cout << "In \\c : " << system->list("\\c") << endl;
        cout << "In \\c\\cn : " << system->list("\\c\\cn") << endl;
        cout << "In \\a\\c : " << system->list("\\a\\c") << endl;
        cout << "In \\a\\c\\cn : " << system->list("\\a\\c\\cn") << endl;
        cout << system->getFile("\\c\\cn", "cnf")->getPath() << endl;
        cout << "Move result = " << system->move("\\","c","\\a","c") << endl;
        cout << "In \\ : " << system->list("\\") << endl;
        cout << "In \\a : " << system->list("\\a") << endl;
        cout << "In \\b : " << system->list("\\b") << endl;
        cout << "In \\c : " << system->list("\\c") << endl;
        cout << "In \\c\\cn : " << system->list("\\c\\cn") << endl;
        cout << "In \\a\\c : " << system->list("\\a\\c") << endl;
        cout << "In \\a\\c\\cn : " << system->list("\\a\\c\\cn") << endl;
        cout << system->getFile("\\a\\c\\cn", "cnf")->getPath() << endl;
Last edited on
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
cout << "================= Test 17 =================" << endl;
        delete system;
        cout << "System deleted. I shall make sure there is no memory leak." << endl;

        cout << "==========================================" << endl;
        cout << endl;
        cout << endl;
    }

    {
        cout << "Testing LLDirectory below..." << endl << endl;

        cout << "================= Test 1 =================" << endl;
        Directory* r = new LLDirectory("\\", NULL);
        cout << r->addChild(true, "a") << endl;
        cout << r->addChild(true, "b") << endl;
        cout << r->addChild(false, "f1") << endl;
        cout << r->addChild(false, "f2") << endl;
        cout << r->addChild(false, "f3") << endl;
        cout << r->addChild(true, "c") << endl;
        cout << r->addChild(true, "c") << endl;
        cout << r->addChild(true, "z") << endl;
        string s = r->list();
        cout << r->list() << endl;
        if(s != "[a] [b] [c] f1 f2 f3 [z]")
            cout << "Format/output is incorrect! Please check carefully. \nYour output= <" << s << "> \nExpected=    <" << "[a] [b] [c] f1 f2 f3 [z]" << ">" << endl;

        cout << "================= Test 2 =================" << endl;
        cout << r->removeChild("b") << endl;
        cout << r->removeChild("nope") << endl;
        cout << r->list() << endl;

        cout << "================= Test 3 =================" << endl;
        cout << r->getPath() << endl;

        cout << "================= Test 4 =================" << endl;
        cout << (r->getChild("nope")==NULL?"NULL":r->getChild("nope")->getName()) << endl;
        cout << (r->getChild("c")==NULL?"NULL":r->getChild("c")->getName()) << endl;

        cout << "================= Test 5 =================" << endl;
        Directory* c = dynamic_cast<Directory*>(r->getChild("c"));
        cout << c->addChild(true, "ca") << endl;
        cout << c->addChild(true, "cn") << endl;
        cout << c->addChild(false, "cf1") << endl;
        cout << c->list() << endl;

        cout << "================= Test 6 =================" << endl;
        cout << c->getPath() << endl;
        cout << c->getChild("cn")->getPath() << endl;
        cout << c->getChild("cf1")->getPath() << endl;

        cout << "================= Test 7 =================" << endl;
        delete r;
        cout << "LLDirectory deleted. I shall make sure there is no memory leak." << endl;

        cout << "================= Test 8 =================" << endl;
        System<LLDirectory>* system = new System<LLDirectory>;
        cout << system->isValidPath("") << endl;
        cout << system->isValidPath("c") << endl;
        cout << system->isValidPath("\\") << endl;
        cout << system->isValidPath("\\c") << endl;
        cout << system->isValidPath("\\c\\cn") << endl;
        cout << system->isValidPath("\\c\\nope") << endl;

        cout << "================= Test 9 =================" << endl;
        cout << system->addDir("\\", "a") << endl;
        cout << system->addDir("\\", "b") << endl;
        cout << system->addFile("\\", "f1", "hi") << endl;
        cout << system->addFile("\\", "f2", "hello") << endl;
        cout << system->addFile("\\", "f3", "howdy") << endl;
        cout << system->addDir("\\", "c") << endl;
        cout << system->addDir("\\", "c") << endl;
        cout << "In \\ : " << system->list("\\") << endl;

        cout << "================= Test 10 =================" << endl;
        cout << system->del("\\", "b") << endl;
        cout << system->del("\\", "nope") << endl;
        cout << "In \\ : " << system->list("\\") << endl;

        cout << "================= Test 11 =================" << endl;
        cout << (system->getDirectory("\\")==NULL?"NULL":"\\") << endl;
        cout << (system->getDirectory("\\c")==NULL?"NULL":"c") << endl;
        cout << (system->getDirectory("\\nope")==NULL?"NULL":"nope") << endl;

        cout << "================= Test 12 =================" << endl;
        cout << system->getDirectory("\\")->getPath() << endl;
        cout << system->getDirectory("\\c")->getPath() << endl;

        cout << "================= Test 13 =================" << endl;
        cout << system->addDir("\\c", "ca") << endl;
        cout << system->addDir("\\c", "cn") << endl;
        cout << system->addFile("\\c", "cf1", "yo") << endl;
        cout << "In \\c : " << system->list("\\c") << endl;
        cout << (system->getFile("\\c", "cfnope")?"nope":"NULL") << endl;
        cout << system->getFile("\\c", "cf1")->getContent() << endl;
        system->getFile("\\c", "cf1")->setContent("yoooooooo");
        cout << system->getFile("\\c", "cf1")->getContent() << endl;

        cout << "================= Test 14 =================" << endl;
        cout << system->getDirectory("\\c\\cn")->getPath() << endl;
        cout << system->getFile("\\c", "cf1")->getPath() << endl;

        cout << "================= Test 15 =================" << endl;
        cout << "In \\ : " << system->list("\\") << endl;
        cout << "In \\a : " << system->list("\\a") << endl;
        cout << "In \\b : " << system->list("\\b") << endl;
        cout << "In \\c : " << system->list("\\c") << endl;
        cout << "In \\c\\cn : " << system->list("\\c\\cn") << endl;
        cout << "Move result = " << system->move("\\","f2","\\c","f2") << endl;
        cout << "In \\ : " << system->list("\\") << endl;
        cout << "In \\a : " << system->list("\\a") << endl;
        cout << "In \\b : " << system->list("\\b") << endl;
        cout << "In \\c : " << system->list("\\c") << endl;
        cout << "In \\c\\cn : " << system->list("\\c\\cn") << endl;

        cout << "================= Test 16 =================" << endl;
        cout << system->addFile("\\c\\cn","cnf","so deep!") << endl;
        cout << "In \\ : " << system->list("\\") << endl;
        cout << "In \\a : " << system->list("\\a") << endl;
        cout << "In \\b : " << system->list("\\b") << endl;
        cout << "In \\c : " << system->list("\\c") << endl;
        cout << "In \\c\\cn : " << system->list("\\c\\cn") << endl;
        cout << "In \\a\\c : " << system->list("\\a\\c") << endl;
        cout << "In \\a\\c\\cn : " << system->list("\\a\\c\\cn") << endl;
        cout << system->getFile("\\c\\cn", "cnf")->getPath() << endl;
        cout << "Move result = " << system->move("\\","c","\\a","c") << endl;
        cout << "In \\ : " << system->list("\\") << endl;
        cout << "In \\a : " << system->list("\\a") << endl;
        cout << "In \\b : " << system->list("\\b") << endl;
        cout << "In \\c : " << system->list("\\c") << endl;
        cout << "In \\c\\cn : " << system->list("\\c\\cn") << endl;
        cout << "In \\a\\c : " << system->list("\\a\\c") << endl;
        cout << "In \\a\\c\\cn : " << system->list("\\a\\c\\cn") << endl;
        cout << system->getFile("\\a\\c\\cn", "cnf")->getPath() << endl;

        cout << "================= Test 17 =================" << endl;
        delete system;
        cout << "System deleted. I shall make sure there is no memory leak." << endl;

        cout << "===========================================" << endl;
        cout << endl;
        cout << endl;
    }

    return 0;
} 
Last edited on
This is the implementation of SADirectory header file.
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#ifndef SADIRECTORY_H
#define SADIRECTORY_H

#include "Directory.h"

const int DIR_SIZE = 8; //the maximum number of children a directory can have for SADirectory

//Static array directory
//it is a directory that is backed by a static array of pointers
//you should read the Directory.h first to see what each member function should do
class SADirectory : public Directory
{
public:
    //constructor
    //hint: remember to initialize all data members properly! C++ doesn't give default values to uninitialized data members
    SADirectory(string name, FSObject* parent);

    //destructor
    //delete all children
    //avoid memory leak
    virtual ~SADirectory();

    //add a child
    //(please read Directory.h first)
    //for SADirectory, you would also simply do nothing and return false
    //if the number of children is already at DIR_SIZE (i.e. no space to add anymore)
    //for SADirectory, when you add a child, always add it to the next empty slot of the array
    //e.g., if there are 3 children, they should occupy children[0], children[1], and children[2]
    //in other words, N children should occupy the first N slots of the array
    //the latest child should have the biggest index
    //hint: you need to create either a new SADirectory or new File object here for adding a child
    virtual bool addChild(bool isDir, string name);

    //remove a child
    //make sure the remaining N children occupy the first N slots of the array
    //so some moving of the elements may need to be performed
    //hint: need to delete a child to avoid memory leak
    virtual bool removeChild(string name);

    //return a child by name
    virtual FSObject* getChild(string name);

    //return a child by index
    virtual FSObject* getChild(int i);

    //return the string that represents the list of children in this directory
    //for SADirectory, the order of the items is simply the array element order
    //i.e., children[0] children[1] ... children[count-1]
    //in other words, the latest child always appears last
    //simply return "<empty>" if the directory is empty (i.e., has no child)
    virtual string list();

    //return the number of children
    virtual int getCount();

private:
    FSObject* children[DIR_SIZE]; //the array of child pointers
    //N children should always occupy the first N slots of the array

    int count; //number of children; should be 0 initially
};

#endif  
Last edited on
This the implementation of the system header file.
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#ifndef SYSTEM_H
#define SYSTEM_H

#include <string>
#include "Directory.h"
using namespace std;

//The file system
//It is a template class where T is either SADirectory or LLDirectory for this assignment
//For simplicity, we won't use both SADirectory and LLDirectory in the same system, i.e.
//System<SADirectory> will only use SADirectory while System<LLDirectory> will only use LLDirectory
template <typename T>
class System
{
private:
    T* root; //the pointer that points to the root directory (i.e. the only top-level directory)

public:
    //constructor
    //initialize the root directory here
    //see FSObject.h for the properties of root
    System();

    //destructor
    //delete the root
    //avoid memory leak
    ~System();

    //return true if the given path is valid; return false otherwise
    //a path is valid if and only if the length of the path is at least 1 and the path starts with the slash character '\\'
    bool isValidPath(string path);

    //return the root pointer
    Directory* getRoot();

    //return the directory given the path
    //notice that the path here includes also the directory name
    //e.g. with path being "\\a\\b", you should return a pointer to the directory "b" under "a" which is under the root "\\"
    //return NULL if the path is invalid according to isValidPath
    //return NULL if there is no directory at the given path
    Directory* getDirectory(string path);

    //return the file given by the path and name
    //e.g. if path is "\\a" and name is "b", you should return the pointer to file "b" under "a" which is under the root "\\"
    //return NULL if the given path is invalid according to isValidPath
    //return NULL if the given path does not indicate an existing directory
    //(e.g., in our example, if "a" doesn't exist under root or is not a directory, return NULL)
    //return NULL if the child of the given name under the given path doesn't exist or is not a file
    //(e.g., in our example, if "b" doesn't exist under "\\a" or is not a file, return NULL)
    File* getFile(string path, string name);

    //add a new directory (of type T) of the given name to the given path, and return 0
    //e.g. if path is "\\a" and name is "b", you should create a new directory named "b" under "a" which is under the root "\\"
    //however, do nothing and return a negative number instead according to the following if applicable
    //return -1 if the given path is invalid according to isValidPath
    //return -2 if the given path does not indicate an existing directory
    //(e.g., in our example, if "a" doesn't exist under root or is not a directory, return NULL)
    //return -3 if the addChild function of the directory of the given path returns false
    int addDir(string path, string name);

    //add a new file of the given name to the given path, also set its content according to the parameter, and return 0
    //e.g. if path is "\\a" and name is "b", you should create a new file named "b" under "a" which is under the root "\\",
    //and set the content accordingly
    //however, do nothing and return a negative number instead according to the following if applicable
    //return -1 if the given path is invalid according to isValidPath
    //return -2 if the given path does not indicate an existing directory
    //(e.g., in our example, if "a" doesn't exist under root or is not a directory, return NULL)
    //return -3 if the addChild function of the directory of the given path returns false
    int addFile(string path, string name, string content);

    //return the string that represents the list of children in the directory specified in the given path
    //notice that the path here includes also the directory name (see getDirectory comment)
    //however, return "" instead if the given path is invalid according to isValidPath
    //or return "<nope>" instead if the given path does not indicate an existing directory
    //or return "<empty>" if the directory is empty (i.e., has no child)
    string list(string path);

    //remove the child of the given name under the given path, and return 0
    //however, do nothing and return a negative number instead according to the following if applicable
    //return -1 if the given path is invalid according to isValidPath
    //return -2 if the given path does not indicate an existing directory
    //return -3 if the removeChild function of the directory of the given path returns false
    int del(string path, string name);

    //move a child of the name fromName under the directory of path fromPath
    //to the directory of path toPath; the child will have the name toName at the destination
    //you should study the sample output carefully to see what is expected
    //if the source item (the fromName under fromPath) is a directory,
    //then all its children will also be moved accordingly
    //if the move is successful, return 0 at the end
    //however, do nothing and return a negative number instead according to the following if applicable
    //return -1 if any of the given fromPath and toPath is invalid according to isValidPath
    //return -2 if any of the fromPath directory and toPath directory is not an existing directory
    //for simplicity, you can assume there is NO other move failure scenario for this assignment
    //you can also assume that we will not try to move a directory to some subdirectory in itself
    //hint: you may use recursion
    int move(string fromPath, string fromName, string toPath, string toName);
};

#include "System.tpp"

#endif  
Last edited on
This is the implementation of System.tpp file

1
2
3
4
//SUBMIT this file
//complete all the missing System implementations here
//remember to put in dummy/empty implentation of all functions here even if you cannot finish them
//it is very important for you to make sure your submitted code can be compiled with the unmodified version of the header files and main.cpp 



Last edited on
This is the implementation of FSObject.cpp file that I have written but I guess I have to use the concept of BST(Binary Search tree)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//complete all the missing FSObject implementations here
//remember to put in dummy/empty implentation of all functions here even if you cannot finish them
//it is very important for you to make sure your submitted code can be compiled with the unmodified version of the header files and main.cpp

#include "FSObject.h"

//do NOT modify the following line of the destructor implementation, it is already completed
FSObject::~FSObject() {}; //we still have to define it although it is pure virtual since the subclass object destruction will still need to use it 

//add your code for FSObject::getPath below


string FSObject::getPath()
{
	char const *path_seperator = "\\";
	std::string path {name};
	for(auto current = this->parent; current; current=current->parent)
	{
		path = current->name + (current->parent? path_seperator : "") + path;
	}
	return path;
}
Last edited on
@salem c I have attached all files for the assignment that i am working on and I am facing problem with the logic implementation of lldirectory, sadirectory, fsobject and system.tpp file. I would be grateful if you could help me in anyway. Thank you so much.
Mmm, so can you edit all your posts now to put [code][/code] tags around all your code.

I didn't spot any error messages in that dump.

Nor does it really work as a "simple self contained example" demonstration of the problem.
I understand @salem c but due shortage of time I couldn't do that. Apologies. Would appreciate if you could help me in implementation of lldirectory.cpp, or saddirectory.cpp, or fsobject.cpp, or system.tpp, whatever you're comfortable with, thank you so much.
Last edited on
Your first problem was with an error message with just one line of code.

I still don't see an error on that particular line of your constructor, when compiling with
g++ -c -std=c++11 *.cpp

Now you want someone to "help" with implementing ALL the functions in all your posted header files.

Sorry, that's not for me.

The first thing you can do is create all the necessary .cpp files, then add all the functions as place-holders, eg.
1
2
LLDirectory::LLDirectory(string name, FSObject* parent) {
}

to the point where you can do
g++ -std=c++11 *.cpp
and not get pages of linker errors "undefined reference to ...".

Then you take your main.cpp and make it look like this
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
#include <iostream>
#include "System.h"
#include "SADirectory.h"
#include "LLDirectory.h"

using namespace std;

int main()
{
    cout << boolalpha;

    {
        cout << "Testing SADirectory below..." << endl << endl;

        cout << "================= Test 1 =================" << endl;
        Directory* r = new SADirectory("\\", NULL);
#if 0
        cout << r->addChild(true, "a") << endl;
        /// snipped for posting
#endif
    }

    {
#if 0
        cout << "Testing LLDirectory below..." << endl << endl;
        /// snipped for posting
#endif
    }

    return 0;
} 

Then you enter an iterative process of moving the first #if 0 line slowly through your code to exercise the next bit of functionality, and implementing the required functionality to make the test pass.


> but due shortage of time I couldn't do that
But you're willing to wait many hours for a forum reply, potentially far longer than it would take to make some real progress on your assignment.

Time management is also an important life skill, so consider the position you're in as a valuable lesson in that direction as well.
Post the exact error message and source file.

1
2
3
.cpp implementation:

LLDirectory::LLDirectory(string name="", FSObject*parent=NULL): FSObject(name,parent){}
Default paramter can only be used by the prototypes (within the class). In the implementation they are useless/not allowed and that's what the error probably tell you.
Topic archived. No new replies allowed.