S
sandy
I am trying to make a simulated directory structure application for a
course.
I am using a Vector to store pointers to my Directory objects (as
subdirectories of the current object).
In my header it looks like this (private section)
vector<Directory*> Directories;
In my cpp file in the constructor I have this:
Directories.clear();
To add directories to the Vector I have a function that asks for name
of the directory and checks it for validity etc. then passes it off to
create and store the new subdirectory using this function:
bool Directory::Create(string DN) //pass in directory name
{
Directory *ND = new Directory(this, DN);
Directories.push_back(ND);
NumSubDirs++;
sort(Directories.begin(), Directories.end());
return true;
}
As you can see I am calling the sort function. As you may guess from
the fact that I am posting the sort does not work. I am 99% sure it's
because I need to come up with the correct method of overloading the <
operator and the > operator.
When sort is called the values change position, they just aren't sorted
by directory name as requested.
I have the following overloaded operator< and operator> functions. (I
am not including the headers) which do NOT do the trick.
bool Directory:
perator<(const Directory & D)
{
return D.Name < Name;
}
bool Directory:
perator<(const Directory *D)
{
return D->Name < this->Name;
}
/*
operator>
*/
bool Directory:
perator>(const Directory & D)
{
return Name > D.Name;
}
bool Directory:
perator>(const Directory * D)
{
return D->Name > Name;
}
I am sure that's the problem, I just don't know how to tell it the
correct thing to do...
Thanks.
course.
I am using a Vector to store pointers to my Directory objects (as
subdirectories of the current object).
In my header it looks like this (private section)
vector<Directory*> Directories;
In my cpp file in the constructor I have this:
Directories.clear();
To add directories to the Vector I have a function that asks for name
of the directory and checks it for validity etc. then passes it off to
create and store the new subdirectory using this function:
bool Directory::Create(string DN) //pass in directory name
{
Directory *ND = new Directory(this, DN);
Directories.push_back(ND);
NumSubDirs++;
sort(Directories.begin(), Directories.end());
return true;
}
As you can see I am calling the sort function. As you may guess from
the fact that I am posting the sort does not work. I am 99% sure it's
because I need to come up with the correct method of overloading the <
operator and the > operator.
When sort is called the values change position, they just aren't sorted
by directory name as requested.
I have the following overloaded operator< and operator> functions. (I
am not including the headers) which do NOT do the trick.
bool Directory:
{
return D.Name < Name;
}
bool Directory:
{
return D->Name < this->Name;
}
/*
operator>
*/
bool Directory:
{
return Name > D.Name;
}
bool Directory:
{
return D->Name > Name;
}
I am sure that's the problem, I just don't know how to tell it the
correct thing to do...
Thanks.