【发布时间】: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 = &nodes[_node];。
标签: c++ data-structures graph