#include <iostream>
#include <windows.h>
#include <cstring>
#include <new>
#include <vector>
#include <stack>
using namespace std;
struct node
{
string value;
vector <node *> subnodes;
}; //end node
class directorytree
{
private:
stack <HANDLE> hstack;
stack <WIN32_FIND_DATA> fdstack;
HANDLE h;
WIN32_FIND_DATA fd;
node thenode;
string directory;
void createtree(node*, string);
void preorder(node *);
void deletetree(node *, node*);
public:
directorytree(node, string);
void classmain();
}; //end directorytree
directorytree::directorytree(node n, string d)
{
thenode = n;
directory = d;
} //end constructor
void directorytree::createtree(node * a, string dir)
{
if (a != 0)
{
a->value = dir;
h = FindFirstFile((a->value + "/*.").c_str(), &fd);
while ((((string)fd.cFileName == ".") || ((string)fd.cFileName == "..")) && (FindNextFile(h,&fd)))
{
} //end while
if (((string)fd.cFileName != ".") && ((string)fd.cFileName != ".."))
{
if (fd.dwFileAttributes != 16)
{
a->subnodes.push_back(0);
createtree(a->subnodes[0], dir);
} //end if
else
{
hstack.push(h);
fdstack.push(fd);
do
{
dir = a->value + '/' + (string)((fdstack.top()).cFileName);
a->subnodes.push_back(new(nothrow)node);
createtree(a->subnodes.back(), dir);
} //end do
while (FindNextFile(hstack.top(), &(fdstack.top())));
hstack.pop();
fdstack.pop();
} //end else
} //end if
else
{
a->subnodes.push_back(0);
createtree(a->subnodes[0], dir);
} //end else
} //end if
} //end createtree
void directorytree::preorder(node * a)
{
if (a != 0)
{
cout << a->value << endl;
if (a->subnodes.size() == 1)
{
preorder(a->subnodes[0]);
} //end if
else
{
for (int pos = 0; pos < (int)(a->subnodes.size()); pos++)
{
preorder(a->subnodes[pos]);
} //end for
} //end else
} //end if
} //end preorder
void directorytree::deletetree(node * a, node * parent)
{
if (a != 0)
{
for(unsigned pos = 0; pos < a->subnodes.size(); ++pos)
{
deletetree(a->subnodes[pos], parent);
} //end for
if (a != parent)
{
cout << "The node for "<<a->value << " got deleted."<<endl;
delete a;
} //end if
} //end if
} //end deletetree
void directorytree::classmain()
{
createtree(&thenode, directory);
preorder(&thenode);
deletetree(&thenode, &thenode);
} //end classmain
int main()
{
node tree;
directorytree dirtree(tree, "C:/Users/owner/Desktop");
dirtree.classmain();
return 0;
} //end main
Using multiple files in C++
Page 1 of 15 Replies - 187 Views - Last Post: 05 January 2013 - 05:00 PM
#1
Using multiple files in C++
Posted 05 January 2013 - 11:39 AM
Any ideas on how I should make this a multi-file project (I am using Code::Blocks and the GCC compiler):
Replies To: Using multiple files in C++
#2
Re: Using multiple files in C++
Posted 05 January 2013 - 12:08 PM
Move the declaration of the directorytree class and the node struct into a header file. Then move the member function definitions into a .cpp file with the same name as the header. Now the file with the main() function should only have the main() function in it.
Also, don't put any "using directives" inside the header file because it can cause subtle problems/bugs that can be hard to track down. What I mean about a "using directive", is the line using namespace std; that you have in the main file. It's alright to use it there and in the implementation file (the .cpp files), just not in the header file.
Also, don't put any "using directives" inside the header file because it can cause subtle problems/bugs that can be hard to track down. What I mean about a "using directive", is the line using namespace std; that you have in the main file. It's alright to use it there and in the implementation file (the .cpp files), just not in the header file.
#3
Re: Using multiple files in C++
Posted 05 January 2013 - 12:14 PM
Here is my attempt:
directorytree.h:
directorytree.cpp
main.cpp
It is throwing the following error:
fatal error: include\directorytree.h : No such file or directory Ironically, that is the only error. (Oh, and thank you vividexstance for your help on that data problem. I made a tutorial out of it.)
directorytree.h:
#ifndef _DIRECTORYTREE_H_
#define _DIRECTORYTREE_H_
struct node
{
std::string value;
std::vector <node *> subnodes;
}; //end node
class directorytree
{
private:
std::stack <HANDLE> hstack;
std::stack <WIN32_FIND_DATA> fdstack;
HANDLE h;
WIN32_FIND_DATA fd;
node thenode;
std::string directory;
void createtree(node*, std::string);
void preorder(node *);
void deletetree(node *, node*);
public:
directorytree(node, std::string);
void classmain();
};
#endif // DIRECTORYTREE_H
directorytree.cpp
#include "include\directorytree.h"
directorytree::directorytree(node n, string d)
{
thenode = n;
directory = d;
} //end constructor
void directorytree::createtree(node * a, string dir)
{
if (a != 0)
{
a->value = dir;
h = FindFirstFile((a->value + "/*.").c_str(), &fd);
while ((((string)fd.cFileName == ".") || ((string)fd.cFileName == "..")) && (FindNextFile(h,&fd)))
{
} //end while
if (((string)fd.cFileName != ".") && ((string)fd.cFileName != ".."))
{
if (fd.dwFileAttributes != 16)
{
a->subnodes.push_back(0);
createtree(a->subnodes[0], dir);
} //end if
else
{
hstack.push(h);
fdstack.push(fd);
do
{
dir = a->value + '/' + (string)((fdstack.top()).cFileName);
a->subnodes.push_back(new(nothrow)node);
createtree(a->subnodes.back(), dir);
} //end do
while (FindNextFile(hstack.top(), &(fdstack.top())));
hstack.pop();
fdstack.pop();
} //end else
} //end if
else
{
a->subnodes.push_back(0);
createtree(a->subnodes[0], dir);
} //end else
} //end if
} //end createtree
void directorytree::preorder(node * a)
{
if (a != 0)
{
cout << a->value << endl;
for (unsigned pos = 0; pos < a->subnodes.size(); pos++)
{
preorder(a->subnodes[pos]);
} //end for
} //end if
} //end preorder
void directorytree::deletetree(node * a, node * parent)
{
if (a != 0)
{
for(unsigned pos = 0; pos < a->subnodes.size(); ++pos)
{
deletetree(a->subnodes[pos], parent);
} //end for
if (a != parent)
{
cout << "The node for "<<a->value << " got deleted."<<endl;
delete a;
} //end if
} //end if
} //end deletetree
void directorytree::classmain()
{
createtree(&thenode, directory);
preorder(&thenode);
deletetree(&thenode, &thenode);
} //end classmain
main.cpp
#include <iostream>
#include <windows.h>
#include <cstring>
#include <new>
#include <vector>
#include <stack>
#include "include\directorytree.h"
using namespace std;
int main()
{
node tree;
directorytree dirtree(tree, "C:/Users/owner/Desktop");
dirtree.classmain();
return 0;
} //end main
It is throwing the following error:
fatal error: include\directorytree.h : No such file or directory Ironically, that is the only error. (Oh, and thank you vividexstance for your help on that data problem. I made a tutorial out of it.)
This post has been edited by IceHot: 05 January 2013 - 12:43 PM
#4
Re: Using multiple files in C++
Posted 05 January 2013 - 04:10 PM
It keeps telling me that directorytree.h doesn't exist whether I use
#include "directorytree.h"or
#include "include\directorytree.h"
#5
Re: Using multiple files in C++
Posted 05 January 2013 - 04:57 PM
Where is directorytree.h located?
Where is your directorytree.cpp located?
Where is your main.cpp located?
Did you make sure that you spelled these file names correctly, including any differences in capitalization.
Also your error message should be telling you in what file that is causing the problem, main.cpp or directorytree.cpp. Please post the complete error message exactly as it appears in your development environment.
Jim
Where is your directorytree.cpp located?
Where is your main.cpp located?
Did you make sure that you spelled these file names correctly, including any differences in capitalization.
Also your error message should be telling you in what file that is causing the problem, main.cpp or directorytree.cpp. Please post the complete error message exactly as it appears in your development environment.
Jim
This post has been edited by jimblumberg: 05 January 2013 - 05:00 PM
#6
Re: Using multiple files in C++
Posted 05 January 2013 - 05:00 PM
Got it! They weren't in the same folder (stupid default Code::Blocks settings!) so I just specified the entire pathname in both directorytree.cpp and main.cpp. Problem solved, lesson learned, and hope restored in the same accomplishment!
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|