Subclass unable to access protected **bool of superclass

Last edited on
Question 1:
What's with the malloc? Try to avoid mixing C and C++ please. ( http://en.wikipedia.org/wiki/New_%28C%2B%2B%29 )

Question 2:
Why is Dfs a subclass of level? Sorry, but do you comprehend what subclassing implies? It means Dfs IS a level. As that is most certainly not the case, don't do it. If you're going to use object orientation, at least do it right.

Question 3:
How do you use Nfs? As you already demonstrated that you do not really understand how object orientation works, I'd dare to say that you just do it wrong.
Last edited on
Um, sorry but my answers were simply based on what I saw. I saw a pretty messy setup, that is using inheritance when the appropitiate approach would be composition. That wasn't "rude", I was simply pointing out that the whole initial situation is pretty messy.

If you still think that anything I wrote was "rude", please point out the parts in which I did not respond appropitiately. I try to be objective and helpful, apparently I am creating a false impression of what I am trying to say. Even if I knew you were an university student, that wouldn't have changed my response.


Now, let's go back to the topic at hand:
For the whole thing to work as intended, you'd have to create your dfs object with it's copy constructor, the argument being the level object- that's what I meant when I asked how you use Nfs. Normally you'd write an own copy constructor here to avoid the both objects referring to the same array, but apparently in this set up this appears to be the goal - to go back to my previous statement, this is simply not object oriented programming, this is a hack that uses unintended side effects of C++ subclasses. Which really makes me question the point of having a dfs class in the first place.

The solution to your problem is that you probably created a new dfs object out of the blue rather than using the copy constructor, though of course without seeing your actual code I can't say for sure.
Last edited on
Last edited on
 
Dfs pathPlanner(*this);


Should call the default copy constructor, if that doesn't work just write your own:
 
Dfs::Dfs(const Dfs &dfs);


Though the const part is of course a lie if you do it like I think you are trying to do.
Last edited on
Last edited on
Uh yes, of course. Narrowing cast, stupid me. Write an own copy constructor for dfs that takes const level&.
the subclass is unable to access the superclass adjacencyMatrix[i][j] and as such returns false data
That will be a compiler error, not a wrong data. You have a matrix in your derived class (in protected ambit), and you can access that.
Where do you ever fill that adjacent matrix?

I tested the memory addresses between the level and Dfs objects calling to find the memory address at adjacencyMatrix[i][j] and they gave different addresses
How did you test that? Different objects will have different address (even with the same class), the pointer is not shared.
That makes me wonder, what do you have an level object? Why don't just use the derived class?
Could you please post the caller code? (the main function)

About the design. I agree with hanst99, a traverse is not a level.
Consider this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class maze{
//...
  graph g; //your adjacent matrix
public:
  std::list<int> planPath(int start, int goal, traverse &T);
};
class traverse{
//...
public:
  virtual std::list<int> reach(int start, int goal, const graph &g) = 0;
};
class depth_first_search : public traverse{/**/};
class breath_first_search : public traverse{/**/};
class right_hand_in_wall : public traverse{/**/};

Now you could change the mode that you traverse at runtime, just selecting the proper object that will perform the task.

Also:
1
2
3
4
5
6
7
8
9
10
11
bool canMove(int x, int y){ //don't be afraid of whitespace
  if ( (adjacencyMatrix[x][y]) == true) {
    return true;
  } 
  else {
    return false;
  }
}
bool canMove(int x, int y){
  return adjacencyMatrix[x][y]; //clearer, don't you think?
}
Last edited on
I am pretty sure he said his professor forced him to use that setup. I don't know if using that behaviour was enforced by the professor as well, but what Chazz seems to want to do is, create an instance of Dfs, call e.g. dfsIterative, and let that sort the level the dfs was created with. Of course that is breaking all the principles of object orientation, and is basically just implementing a composition with inheritence (seriously, you'd have to be one hell of a confused hacker to think of something like that), but if that's the way Chazz wants or has to do it, why not.

On a side note, is anyone going to ban spoon licker anytime soon? You know, if one has one or two troll posts I guess you can let him pass, but he is taking this a bit too far.
Last edited on
Because it is better to learn how to do it right. It will not be improvement the other way.
Now we can discuss what could be a proper design.

just implementing a composition with inheritence (seriously, you'd have to be one hell of a confused hacker to think of something like that)
I will like to say that I don't enter in that group. But I can't. And the worst part is when "it works".

side note: They have banned it, repeatedly. So I don't think it would last long.
Last edited on
No need for friendship. In fact friendship ask for more coupling than inheritance.

Avoid copying the matrix, just pass it by const reference (you don't need to change it, right?)
1
2
3
class Algorithm{
  virtual list<int> path(node start, node goal, const bool **matrix);
}


You could avoid the conditionals for DFS recursive and DFS iterative, if you derive the two classes from DFS.
Last edited on
I wonder why it don't work with the const. [edit] I guess is the line this->adjacencymatrix = matrix; try making adjacency const too [/edit]

I guess that you've got something like:
1
2
3
4
5
ist<int> Dfs::functionCall(int currentNode, int goalNode, bool **matrix) //what calls the relevant function
{
  this->adjacencymatrix = matrix;
  //...
}

So the problem should be in Level. Try to print the matrix before calling the pathfinder.
Then check the initialization function.

Post more about Dfs::funtionCall and the caller in Level.

By the way, the Algorithm::recursive, Algorithm::iterative, what do they do? they return if the cells are connected?
Last edited on
Topic archived. No new replies allowed.