0
private class Node
 {
    public int Value;
    public Node[] ChildNodes;
    public bool Selected;
 }

The class Node contains an array of Nodes.

Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
Ankan Kumar Giri
  • 243
  • 1
  • 3
  • 12

2 Answers2

0

My C++ is very rusty. Before we tackle the coding there are some things you need to know.

Classes can not directly have access modifiers like public, protected, private or in C++. Meaning we are getting a problem already at private class Node.

This you can go around by using an Nested Class

This brings (psuedo) code to this:

class Outer 
{
    private:
        class Node
        {
            public:
        }
};

We then have to add the the fields, usually (at least used to) in C++ you are using a prefix for variable names, much like capitalizing fields in C#.

int     mValue;
Node*   mNodes;
bool    mSelected;

Which brings us to the final Class:

outer.h

#ifndef OUTER_H
#define OUTER_H

class Outer 
{
    private:
        class Node
        {
            public:
                int     mValue;
                Node*   mNodes;
                bool    mSelected;
        }
};

#endif

More of the "Include guard" can be read here. In some enviroment you can use #pragma once instead of the guards.

I hope this gets you started. This code has not been tested.

This is a bit of XYZ problem, we don't know for what reason you want to write it in C++ instead of C#. Working with C++ can be a hassle. I also (in both cases) recommend to work with a List in C# a Vector in C++ instead of an Array.

Community
  • 1
  • 1
Emz
  • 1,280
  • 1
  • 14
  • 29
  • g++ gives error near ) " expected unqualified-id before ) " in Node – Ankan Kumar Giri Dec 06 '15 at 08:45
  • Remove the parentheses, they are not supposed to be there. My IDE added them for me. Also,please read the post and do not copy-paste it, you would've found the error yourself had you been following the links. I am far from a C++ expert. – Emz Dec 06 '15 at 08:47
  • Thanks, but now it gives error near Node[ ]. " expected unqualified-id before [ " I actually need to implement some graph algorithm in c++. I know c# is hassle free but then I am told to perform the program in c++ itself – Ankan Kumar Giri Dec 06 '15 at 08:51
  • `Node mNodes[];` should be like that in C++. Again. Every error takes a couple of seconds to find if you try. – Emz Dec 06 '15 at 08:55
  • I tried that too previously with every possible combination but it still gives error "mNodes has incompatible type" – Ankan Kumar Giri Dec 06 '15 at 09:01
  • 1
    this works thanks, so I will dynamically allocate memory for the pointer. – Ankan Kumar Giri Dec 06 '15 at 09:07
0

First of all, private classes in C# must be contained within an outer type, so we can assume that this type is already nested (i.e., neither C# nor C++ allows a private class at the namespace level). Next, it's much easier to use std::vector instead of actual arrays:

#include <vector>

//class Outer { - your existing outer class
    class Node
    {
    public:
        int Value = 0;
        std::vector<Node*> ChildNodes;
        bool Selected = false;
    };
//}; - end of your existing outer class
Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28