开发者

Syntax Error: identifier 'Edge'? [closed]

开发者 https://www.devze.com 2023-01-22 03:25 出处:网络
Closed. This question needs debugging details. It is not currently accepting answers. Edit the question to include desired behavior, a specific problem or error, and the shortest code nece
Closed. This question needs debugging details. It is not currently accepting answers.

Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.

Closed 3 years ago.

Improve this question

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)
 {
  nu开发者_Go百科mberOfNodes = nodes;
  numberOfEdges = edges;
  Node = new Node[numberOfNodes];
  Edge = new Edge[numberOfEdges];
 }


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.


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.


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.


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];
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消