I am having a problem writing the last part of my code for this program.
There is a DFS function that I need to write that is included in the header file of the graph.h my program.

Here is the sample of code that my instructor gave me.

template<class VertexType>
void DepthFirstSearch(GraphType<VertexType> graph,
VertexType startVertex, VertexType endVertex)
// Assumes VertexType is a type for which the “==“ and "<<"
// operators are defined
{
using namespace std;
StackType<VertexType> stack;
QueType<VertexType> vertexQ;
bool found = false;
VertexType vertex;
VertexType item;
graph.ClearMarks();
stack.Push(startVertex);
do
{
stack.Pop(vertex);
if (vertex == endVertex)
{
cout << vertex);
found = true;
}
else
{
if (!graph.IsMarked(vertex))
{
graph.MarkVertex(vertex);
cout << vertex;
graph.GetToVertices(vertex, vertexQ);
while (!vertexQ.IsEmpty())
{
vertexQ.Dequeue(item);
if (!graph.IsMarked(item))
stack.Push(item);
}
}
}
} while (!stack.IsEmpty() && !found);
if (!found)
cout << "Path not found." << endl;
}

But when I try to write it I keep getting errors when it compiles.

This is his email to us the students on the DFS function.

DepthFirstSearch() tasks
- DFS requires one stack (static), one queue (static), and one queue (dynamic) to hold the path. Instead of printing out each vertex on the path, DFS queues them by name in the dynamic queue. When DFS ends, it must return the address of the dynamic queue to main() so that main() can complete its tasks. Otherwise, the DFS algorithm follows that presented in the book/notes.

This function is in the graph.cpp file.
I have attached all the files that are used in this program.
What an I doing wrong on this function?

Narrow the problem down a bit and you'll recieve more help.

I don't understand what I am doing wrong in the DFS function. Since the DFS function is is declared in the graph.h file and it is part of the class in the definition, why can't I just call the function in the implementation of the DFS function?
Example: if(!IsMarked(vertex)) Why can't I do this? I have also attached my graph.cpp file. What do I need to do?

you need to have the concept of 'object oriented programing' . A behavior(function) is belong to a object, it can not exist seprately. just like a hand can not do anything if it is not a part of a body....
Thinking in this way you 'll get the idea.
if you still want to use it in traditional way, you can not declare the function in a class.

you need to have the concept of 'object oriented programing' . A behavior(function) is belong to a object, it can not exist seprately. just like a hand can not do anything if it is not a part of a body....
Thinking in this way you 'll get the idea.
if you still want to use it in traditional way, you can not declare the function in a class.

Not quite.

You can have behavior functions via the 2nd and 3rd levels of polymorphism also.

The 3 (or maybe '4') levels of Polymorphism that I know of are-

1.) Virtual
2.) Static
3.) Callback
[4.) Delegation]

- the 4th is questionable since I've only recently heard about it and it seems to be an implementation in only a select few languages @_@.

You can have a Callback method that uses polymorphism via calling some function that meets the required signature (return, parameters, and class/nonclass info, and any other info regarding if the method is meant for const and non-const objects or just non-const objects... possibly even calling convention... possibly even the restriction of the scope of the function (though I don't think I've encountered that issue), etc). Of course the method may not have the same name as another but it will have enough similar traits to be invoked as a generalized type at run-time.

There's also Static polymorphism. This is typical with template-types where method and member lookup for an object is resolved at compile time. I'm still a bit fuzzy on this topic, but from what I understand you can use any class as a template argument for a class so long as it meets the minimum requirements of operations used on the template. For example, if the generalized-type is constructed anywhere as a temporary reference for the execution of a method, the class argument that is standing in place of the templated type must have its default constructor defined... etc.

Delegation is a topic I can't explain >_<. You'd have to look it up, and potentially experiment with it to understand it @_@.

So in a nutshell you can have multiple objects with the same behavior functions without using the virtual mechanism, but instead either the static mechanism (which is popular in C++), or the traditional callback mechanism (which was the solution for C-based polymorphism).


@JustLearning

To save us some document-sifting, can you be more elaborate as to what type of error you're getting? What does it say when you try to compile?

Maybe you didnt understand what i mean,
A behavior(function) is belong to a object, it can not exist seprately.
In the situation of this problem, it is definitely correct.

1.) Virtual
2.) Static
3.) Callback
4.) Delegation
the Polymorphism features you mentioned above are also defined in 'object' scope or 'class' scope(e.g.static).
1) virtual function , even if it is a virtual method, its behavior can be overridden within an inheriting class by a function with the same signature. this function must have specific behavior in runtime, at that time , it must belong to a specific object .
2) Static. in this case, a behavior is belong to the whole class , and every object of this class shares this behavior. Although it is not belong to a single object , it BELONG TO a class. it can not exist separately ,either.
3) Callback. Strictly speaking, i dont think callback belongs to OOP, any executable code that is passed as an argument to other code can be a type of callback.
4) Delegation. in c# programing, you can easily find this feature . it seems like a pointer of a function in c/c++. And also, i don't think it contains any OOP features.

Maybe you didnt understand what i mean,
A behavior(function) is belong to a object, it can not exist seprately.
In the situation of this problem, it is definitely correct.

1.) Virtual
2.) Static
3.) Callback
4.) Delegation
the Polymorphism features you mentioned above are also defined in 'object' scope or 'class' scope(e.g.static).
1) virtual function , even if it is a virtual method, its behavior can be overridden within an inheriting class by a function with the same signature. this function must have specific behavior in runtime, at that time , it must belong to a specific object .
2) Static. in this case, a behavior is belong to the whole class , and every object of this class shares this behavior. Although it is not belong to a single object , it BELONG TO a class. it can not exist separately ,either.
3) Callback. Strictly speaking, i dont think callback belongs to OOP, any executable code that is passed as an argument to other code can be a type of callback.
4) Delegation. in c# programing, you can easily find this feature . it seems like a pointer of a function in c/c++. And also, i don't think it contains any OOP features.

Static isn't defined strictly for classes. Static implies that something is resolved at compile-time (or in Java, something is used for the first time (Static Fields)). If this is hard to believe, refer to the use of static in C, a non object-oriented language where static referred to (in short) 'as part of this file' and not a shared member of instances of an object of the class-type.

It is possible to use different functions (different behavior) from different files (non-class types, but the concept is similar) using a combination of Static and Callback polymorphism with functions (behavior in other files) that can both resolve the type at compile time and use the type abstractly in a way that the behavior can still differ among types without the use of classes.

I'd have to do some research on statically calling storage-type members of a namespace via the static mechanism. I wonder if that is also possible (in theory it should be)?

Oh and here's a question for you. Why do you think a Callback is not a valid form of OOP? The virtual mechanism is very similar, except you are relying on a virtual table of a class instead of a reference to a function with the same signature.

I'm honestly not convinced that Polymorphism belongs only to OOP.

I found out what I was doing wrong I put the function heading like this

Queue* DepthFirstSearch(string startVertex, string endVertex);

instead of like this

Queue* Graph::DepthFirstSearch(string startVertex, string endVertex);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.