简体   繁体   English

无法编译C ++代码:从“ Node *”到“ int”的无效转换

[英]Unable to compile C++ code: invalid conversion from 'Node*' to 'int'

I have this C++ class with an array of Node objects called adj (I guess you don't need to see the implementation of my Node class) 我有一个C ++类,其中包含一个称为adj的Node对象数组(我想您不需要查看Node类的实现)

class Graph {
public:
    Node *adj;
    bool *marked;
    int nVertex, p;
    int *distance;

    void graph(int quantity);
    bool is_marked();

    void cleaner();
    void newVertex(int value);
};

And I have this method which creates a node nod and tries to store it in the p-th position in adj : 而且我有这种方法,它创建一个节点nod并尝试将其存储在adj的第p个位置:

void Graph::newVertex(int value)
{
    Node *nod = new Node(value);
    adj[p++] = nod;
}

When I try to compile this code I get the following error message: 当我尝试编译此代码时,出现以下错误消息:

invalid conversion from 'Node*' to 'int'

I can't see what I have done wrong in my code. 我看不到自己的代码做错了什么。 The array initialization looks right to me and the object assignment too. 数组初始化对我和对象分配也很合适。 Please help me answer this question. 请帮助我回答这个问题。

UPDATE: the code for the Node class: 更新: Node类的代码:

class Node {
public:
    int value, cost;
    Node *next;

    Node() {}

    Node(int val) {
        value = val;
        next = NULL;
        cost = 0;
    }
};

UPDATE: I can't use C++ vector here. 更新:我不能在这里使用C ++向量。 I'd love to but it's for a homework thing. 我很乐意,但这是为了做家庭作业。 Before anyone thinks I am cheating or something, please note that I'm not asking for a solution to my specific assigned problem, but instead to a problem that I am having for compiling the code. 在任何人认为我在作弊之前,请注意,我并不是在寻求解决我分配的特定问题的方法,而是要解决我在编译代码时遇到的问题。

The type of adj[p++] is clearly Node& and you try to assign a Node* to it. adj[p++]类型显然是Node& ,您尝试为其分配Node* I guess, your Node type has a constructor taking an int and the compiler tries but fails to convert the Node* to an int . 我猜想,您的Node类型有一个采用int的构造函数,而编译器尝试但未将Node*转换为int

You probably meant to declare adj as 您可能打算将adj声明为

std::vector<Node*> adj;

... and then add new nodes using, eg: ...然后使用以下方法添加新节点:

adj.push_back(nod);

(note, that you still need to make sure the allocated objects get released at the appropriate point in time). (请注意,您仍然需要确保在适当的时间点释放分配的对象)。

adj is of type Node* . adj的类型为Node* adj[someIndex] is then of type Node . adj[someIndex]的类型为Node You are trying to assign Node* to Node . 您正在尝试将Node*分配给Node My educated guess is, you have Node::operator=(int) , so the compiler tries to interpret your code that way - but that doesn't work out either, producing the error message you observe. 我的有根据的猜测是,您拥有Node::operator=(int) ,因此编译器尝试以这种方式解释您的代码-但这也不起作用,产生您观察到的错误消息。

OK, with the definition of the Node class in hand I think I can see what you're trying to do. 好的,有了Node类的定义,我想我可以看到您正在尝试执行的操作。 If I'm correct, you want to have Graph::adj point to a linked list of Node elements, with each Node then pointing to the next Node in the list. 如果我是对的,则希望Graph::adj指向Node元素的链接列表,然后每个Node指向列表中的下一个Node If that's correct the implementation of new_vertex needs to look something like: 如果正确,则new_vertex的实现应类似于以下内容:

void Graph::newVertex(int value)
  {
  Node *nod = new Node(value);
  nod->next = adj;
  adj = nod;
  }

No need to have an index ( p ) - you'd just walk the linked list of Node elements with code similar to 无需索引( p )-您只需遍历Node元素的链表,其代码类似于

Node *n = adj;

while(n != NULL)
  {
  // do something useful with n

  n = n->next;
  }

If you really insist upon using array syntax to access elements of the linked list (bad idea in my mind, as it just adds to the potential confusion, but YMMV) you could add something like 如果您真的坚持使用数组语法来访问链表的元素(在我看来,这是个坏主意,因为这只会增加潜在的混乱,但YMMV),则可以添加以下内容:

Node *operator[](int n);  // 0-based index into Node list

to Graph with an implementation similar to Graph的实现类似于

Node *operator[](int ndx)
  {
  Node *n = adj;

  for( ; n != NULL, ndx > 0 ; ndx--)
    n = n->next;

  return n;
  }

Share and enjoy,. 分享并享受。

After you have created your new Node : 创建新Node

Node *nod = new Node(value);

you need to hook it up to your linked list. 您需要将其连接到链接列表。 A linked list looks like: 链接列表如下所示:

[HEAD] => [value|next] => [value|next] => NULL

where HEAD in your case would be adj . 在您的情况下HEAD将为adj

Therefore, you need to update the next of your new node to point to the current head node, then update the head node to point to the new node. 因此,您需要更新新节点的下一个以指向当前的头节点,然后更新头节点以指向新的节点。

That is, you should end up with something like: 也就是说,您最终应该得到如下结果:

adj => [value|next] => [value|next] => ... => NULL
       ^               ^
       nod             adj'

where adj' is the old value of adj . 其中adj'是的旧值adj

It helps to create diagrams of what the data structure looks like and how the values are being updated. 它有助于创建数据结构外观以及如何更新值的图表。

You then need to work out how to traverse the nodes in the list. 然后,您需要确定如何遍历列表中的节点。

Also, don't forget to clean up the nodes in the Graph destructor (and be careful how you do that). 另外,不要忘了清理Graph析构函数中的节点(并要谨慎行事)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM