How to implement a graph with different types of Nodes?

Question!

I'm a very much new to C++ programming and I'm trying to implement a Scene Graph in C++.

There should be a Node class, which is the super class (and an abstract class) and some child classes like Geometry, Group, Transformation etc. I need to implement a graph, where either of sub class objects can be nodes. There can be one or many children per node. (There are common methods and attributes as well as different methods and attributes in sub classes.)

I need to create a graph where I can add,remove nodes and execute their common methods regardless to their type.

Can anyone please share any ideas or methodologies to implement such graph?

EDIT : This is a sample definitions of my work. (I only add the header contents. But if the implementation is needed, I will provide.)

node.h

using namespace std;

#include <cstdio>
#include <string>
#include <vector>

#ifndef NODE_H
#define NODE_H

class Node{
public:
  Node();
  virtual ~Node() = 0;
  Node(string name);

  string getName();
  void setName(string name);

  vector<Node*> getChildrenNodes();
  size_t getChildNodeCount();
  Node* getChildNodeAt(int i);
  void setChildernNodes(vector<Node*> children);

  void addChildNode(Node* child);

  virtual string getNodeType()=0; // to make this class abstract

protected:
  string name;
  vector<Node*> children;
};
#endif

geometry.h

class Geometry :  public Node {
public:
    Geometry();
    ~Geometry();
    string getNodeType();
    // Method overrides and other class specific methods.
};

graph.h

#include "node.h"

class Graph{
public:
    Graph();
    ~Graph();
    void addNode(Node* parent,Node* child);
    void addNode(Node* child);
    Node* getRoot();
private:
    Node* root;
};

This is my main.cpp

#include <cstdio>
#include <stdio.h>
#include <iostream>

#include "node.h"
#include "graph.h"

int main(){

    Graph sg;
    Geometry g1;

    sg.addNode(g1); // error: no matching function for call to ‘Graph::addNode(Geometry&)’

    return 0;
}

If you can, show where I went wrong.



This video can help you solving your question :)
By: admin