1

I am trying to use OGDF C++ library for a project and want to use a protected member function of a class of this library. I can't access protected members directly outside class or derived class, so to use protected method of Balloon Layout class I created a class A which inherits from BallonLayout. From A, a protected function of the super class is called in a public function abc() of class A; so that I can use abc() outside the class and indirectly protected function of class BallonLayout.

Here's the code, please tell me where there is a problem in it.

#include <ogdf/basic/Graph.h>
#include <ogdf/basic/graph_generators.h>
#include <ogdf/misclayout/BalloonLayout.h>

using namespace ogdf;

class OGDF_EXPORT A : public BalloonLayout{
        public:
            void abc(const Graph &G){
            selectRoot(G); //Calling super class protected method.               
            }
};

int main()
{
    int n = 5, m = 7;
    Graph G;
    ogdf::planarBiconnectedGraph(G, n, m);

    A* a = new A;
    a->abc(G);
    cout << "Done!!";
return 0;

}

It compiles without any error but at run time it gives “Segmentation fault (core dumped)”. This error comes when we try to access something (object/variable) which is not in memory. But I do not understand what mistake have I done.

In place of A* a = new A; a->abc(G);, I tried the following too but I am getting the same error.

A* a; 
a->abc(G);

and

A *a = new A;
a->abc(G);
delete a;

and

A a;
a.abc(G);

Foe each one of the attempts above, I get a segmentation fault. This error is coming after calling a.abc(G) when this method calls the superclass's method.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Shaifali
  • 309
  • 3
  • 9
  • `A* a;` does not point to anything , you should change that do `A* a = new A;` or `A a; a.abc(G);` – Raxvan Mar 13 '14 at 13:00

1 Answers1

4
A* a;
a->abc(G);

That creates a pointer without initialising it; then attempts to dereference that invalid pointer to call a function. The result is a segmentation fault, or other undefined behaviour.

You almost certainly want to create an object:

A a;
a.abc(G);
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • I have tried also but getting same error :( Also used delete a; but not working. – Shaifali Mar 13 '14 at 13:03
  • @Shaifali: Don't use `delete` unless you created the object with `new`. If you've made the change I suggested and it's still crashing, then the error is in some code you haven't posted. – Mike Seymour Mar 13 '14 at 13:09
  • please look at the edited question; I tried all cases but its giving segmentation error at run time for all. – Shaifali Mar 13 '14 at 13:54
  • @Shaifali: If you're still getting a segfault, then it's time to get out the debugger and find where it's happening. There's nothing obviously wrong with your code, once you make this change. – Mike Seymour Mar 13 '14 at 13:56
  • After calling a.abc(G); Geting this fault while calling "selectRoot(G);" that is function of super class. Please can you tell me what to do next? – Shaifali Mar 13 '14 at 16:12
  • @Shaifali: Sorry, I can't help; that's a problem with either the library, or your usage of it, and I don't know anything about the library. – Mike Seymour Mar 13 '14 at 16:26
  • this is link for its documentation of that method if you can figure out my mistake http://www.ogdf.net/doc-ogdf/classogdf_1_1_balloon_layout.html#aff8cd83738568fb26b3ff21aaa466a4e – Shaifali Mar 13 '14 at 16:29