-2

At the consturctor Node = new Node[numberOfNodes]; and Edge = new Edge[numberOfEdges]; gives identifier error? what's the wrong with it ?

typedef struct node
{
 int id;
 int x;
 int y;
} Node;

typedef struct edge
{
 int id;
 Node node1;
 Node node2;
} Edge;

class graph
{
private:
 int numberOfNodes;
 int numberOfEdges;
 int *Node;
 int *Edge;

public:
 graph(int nodes, int edges)
 {
  numberOfNodes = nodes;
  numberOfEdges = edges;
  Node = new Node[numberOfNodes];
  Edge = new Edge[numberOfEdges];
 }
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
anarhikos
  • 1,415
  • 3
  • 15
  • 19

4 Answers4

2

You have various variable name conflicts, including a conflict between your variable declaration int* Node, and the typedef Node. Also, you declare your array of nodes as type int* when it should be type Node*. You do the same with Edge. Try the following instead:

class graph
{
    private:
    int numberOfNodes;
    int numberOfEdges;
    Node* nodes_;
    Edge* edges_;

    public:

    graph(int num_nodes, int num_edges)
    {
        numberOfNodes = num_nodes;
        numberOfEdges = num_edges;
        nodes_ = new Node[numberOfNodes];
        edges_ = new Edge[numberOfEdges];
    }
};

Also, just for the future, the typedef struct { } idiom is really unnecessary in C++. It's main purpose was so that C programmers wouldn't need to constantly have to prefix their struct variables with the word struct. But in C++ this isn't necessary, so there's generally no real reason to say typedef struct node { ... } Node; when you can just say struct Node { ... };. See here for more information.

Community
  • 1
  • 1
Charles Salvia
  • 52,325
  • 13
  • 128
  • 140
0

In your constructor, Node and Edge refer to the int* members of the class, not the type names in the global scope. You will have to indicate that you're referring to the global types:

Node = new ::Node[numberOfNodes];
Edge = new ::Edge[numberOfEdges];

Edit: of course, this would still confuse human readers. It's advised to keep type and variable names separate for the sake of your readers.

Victor Nicollet
  • 24,361
  • 4
  • 58
  • 89
0

The compiler is confused by the fact that you used Node and Edge both as a type identifier (in typedef ... Node;) and as a class member (int *Node). I was confused for a while too. You can't really do that. In general, it's a good idea to choose different names for types, members, and variables that are supposed to be different.

aschepler
  • 70,891
  • 9
  • 107
  • 161
0

I'm sorry I've written nonsense things..it was like that. However I get still erors: Eerror C2440: '=' : cannot convert from 'Edge *' to 'int'

public:
    graph(int nodes, int edges)
    {
        numberOfNodes = nodes;
        numberOfEdges = edges;
        nodes = new Node[numberOfNodes];
        edges = new Edge[numberOfEdges];
    }
anarhikos
  • 1,415
  • 3
  • 15
  • 19