【问题标题】:Compile time error while initializing start object [duplicate]初始化开始对象时编译时间错误[重复]
【发布时间】:2019-01-16 09:13:17
【问题描述】:

我想将一个变量初始化为nodes[_node]的值,但它给出了一个错误。

我试过start = &nodes[_node]; 并没有给出任何错误,但我不想初始化我想要初始化值的地址。

#include <iostream>

using namespace std;

struct  graph
{
    int data;
    struct graph *next;
};

struct graph *nodes =  new graph[5];

void connectnode(int _data,int _node)
{
    struct graph *start = nodes[_node];

    struct graph *temp = new  graph;

    temp ->data = _data;
    temp ->next = nullptr;

    if(start == nullptr)
        start->next = temp;
    else
    {
        while(start != nullptr)
            start = start->next;

        start->next = temp;
    }

}

错误是:

Compile time error - cannot convert 'graph' to 'graph*' in initialization

【问题讨论】:

  • nodes 是一个graph*,所以nodes[_node] 是一个graph 并且不能分配给graph *start。你可能想要struct graph *start = &amp;nodes[_node];

标签: c++ data-structures graph


【解决方案1】:

使用nodes[_node];,您将获得数组graph 的元素,因此您将获得graph 类型。但是,您将此元素分配给 struct graph *start 类型的指针。

所以该行应该是:

struct graph *start = &nodes[_node];

我不明白你为什么不想这样做,但如果是这样你可以试试:

struct graph start = nodes[_node];

【讨论】:

  • 谢谢,我认为nodes[_node] 是指针类型
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-21
  • 2017-08-15
  • 1970-01-01
  • 1970-01-01
  • 2012-01-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多