Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ malloc在图形程序中的应用_C++_Algorithm_Graph Theory - Fatal编程技术网

C++ malloc在图形程序中的应用

C++ malloc在图形程序中的应用,c++,algorithm,graph-theory,C++,Algorithm,Graph Theory,我有以下代码用于使用相邻列表创建图形结构 #include<iostream> #include<algorithm> #include<string.h> #include<math.h> #include<iostream> using namespace std; #define MAX 1000 typedef struct { int y; int weight; struct edgenode *n

我有以下代码用于使用相邻列表创建图形结构

#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<iostream>
using namespace std;
#define MAX 1000

typedef struct {
    int y;
    int weight;
    struct edgenode *next;
    }edgenode;

typedef struct
{
    edgenode* edges[MAX+1];
    int degree[MAX+1];
    int nvertices;
    int ndegrees;
    int nedges;
    bool directed;
    }graph;

void initialize(graph *g,bool directed){
    int i;
    g->nvertices=0;
    g->nedges=0;
    g->directed=directed;
    for(i=1;i<=MAX;i++)g->degree[i]=0;
    for(i=1;i<=MAX;i++)g->edges[i]=NULL;
}

void insert(graph *g,int x,int y,bool directed){
    edgenode *p;
    p=malloc(sizeof(edgenode));
}

int main(){
    return 0;
}

<>我做了什么错误,我怎么解决它?

< p> C++中,你必须把<代码> MalCube()/<代码>的结果转换成正确的类型:

p = (edgenode*)malloc(sizeof(edgenode));
此外,您不应该使用
typedef
,当然,您不应该这样做:

struct edgenode {
    int y;
    int weight;
    struct edgenode *next;
};

<>你的代码看起来像是C和C++的奇怪组合。没有什么能阻止你这么做,但这可能是个坏主意。你应该决定是用C还是C++写。

你使用的<代码> EdGeNoDe</C>不一致:

typedef struct {
    int y;
    int weight;
    struct edgenode *next;
} edgenode;
next
的声明中,您使用类型
struct edgenode
,但是您声明的结构是一个匿名结构,它的类型定义为名称
edgenode
。要将结构引用为
struct edgenode
,必须将该名称添加到声明中:

typedef struct edgenode {
   ...
} edgenode;

第二个错误是因为不能将C++中的代码>空隙*/COD>隐式转换成其他指针类型,所以必须明确地将代码< MalOC 的结果转换成C++,或者使用C++ <代码>新的< /代码>。是的,也许你可以做这项工作,但我会不愿意把结果称为学习C++的成功。我不是从C++学习C++,我想这是清楚的吗?我问过关于图论的问题,而不是C++。我认为从这个代码中可以清楚地看出,如果你打算在C++中编程,你就应该学习C++。问题是关于编译时错误。这意味着这是一个关于C++的问题,而不是图论。KerrekSB:我认为主要的问题是试图用C++编译器编写C。MikeSeymour:我认为诚实的原因应该是“用语言编译器编译语言X”!好的,我知道,但是我正在尝试从书中学习,所以我用它作为书本,你确定这本书是关于C++的吗?如果没有,那么你需要格外小心。(如果是的话,你可能应该把它扔掉。)@dato,那么要么那本书很糟糕,要么你把它和你学到的其他东西混在一起,把它弄得一团糟。
typedef struct edgenode {
   ...
} edgenode;