tags:

views:

37

answers:

4

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];
 }
A: 

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
This would work. But really, don't do it.
aschepler
A: 

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
+2  A: 

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.

Charles Salvia
A: 

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
I've seen my mistake. Thanks for all answers.
anarhikos