我的C++程序可以运行,但总是弹出错误
My C++ program can run but always pop up error
这是我的代码:
GraphBuilder.h
//#pragma once
#include<iostream>
#include<stdio.h>
#include<fstream>
#include <stdlib.h>
#include <string>
using namespace std;
#define MaxVertexNum 500000
struct Node{
int data;
struct Edge *next;
};
struct Edge{
int data;
int weight;
struct Edge* next;
};
class GraphBuilder
{
public:
GraphBuilder();
void CreateGraph();
void printGraph();
Node *header;
int total_of_nodes, total_of_edges;
private:
};
GraphBuilder.cpp
#include"GraphBuilder.h"
using namespace std;
GraphBuilder::GraphBuilder()
{
}
void GraphBuilder::CreateGraph()
{
int i,j,k;
int vex1, vex2, weight;
char a;
Edge *tmp, *newNode;
FILE *fp;
int line= -1;
fp = fopen("Text1.txt", "r");
if(fp == NULL)
{
cout<<"Cannot open file!n";
return;
}
while(!feof(fp))
{
if(line == -1)
{
fscanf(fp, "%d %d", &total_of_nodes, &total_of_edges);
line++;
}
else break;
}
for(i=0;i<total_of_nodes;i++)
{
header[i].data = i;
header[i].next = NULL;
}
while(!feof(fp))
{
if(line == -1)
{
fscanf(fp, "%d %d", &total_of_nodes, &total_of_edges);
line++;
}
else
{
fscanf(fp, "%d %d %d", &vex1, &vex2, &weight);
newNode = (Edge *)malloc(sizeof(Edge));
newNode->data = vex2;
newNode->weight = weight;
newNode->next = NULL;
if (header[vex1].next == NULL)
header[vex1].next = newNode;
else
{
tmp = header[vex1].next;
header[vex1].next = newNode;
newNode->next = tmp;
}
}
}
}
void GraphBuilder::printGraph()
{
int i;
Edge* tmp;
for (i=0; i<total_of_nodes; i++)
{
cout<<header[i].data;
if (header[i].next != NULL)
{
tmp = header[i].next;
cout<<"->"<<tmp->data;
while (tmp->next != NULL)
{
cout<<"->"<<tmp->data;
}
}
cout<<endl;
}
}
main.cpp
#include"GraphBuilder.h"
using namespace std;
void main()
{
GraphBuilder gb;
gb.CreateGraph();
gb.printGraph();
}
我在VS2012上运行代码,它总是弹出发生访问冲突的错误。我不知道为什么会出现这个错误,我是C++的大一新生。请告诉我如何更正我的代码。谢谢你的帮助。
您的编译器没有警告您吗?
const int total_of_nodes = 0, , total_of_edges = 0;
和
fscanf(fp, "%d %d", &total_of_nodes, &total_of_edges);
这不太好。您正在修改const对象,它是未定义的行为。
您的访问违规问题来自于访问您的头数组而之前没有为其分配空间:
for(i=0;i<total_of_nodes;i++)
{
header[i].data = i;
header[i].next = NULL;
}
使用动态分配方式:
Node *header;
...
header=(Node*) malloc(SIZE*sizeof(Node));
或者
Node *header = new Node[SIZE];
或者使用静态分配您的头
Node *header[SIZE];
似乎从未分配过header
。
除非我错过了,否则您似乎还没有初始化您的"header"变量,所以
for(i=0;i<total_of_nodes;i++)
{
header[i].data = i;
header[i].next = NULL;
}
可能会导致一些错误,因为标头[i]可能指向任何位置。。。
您必须初始化标题列表。
您应该在使用指针之前分配它,否则即使程序可以成功编译,也会出现运行时错误。
相关文章:
- 分段错误当我试图运行程序时出错
- .h 和.cpp文件分离时出错,但仅使用 .h 文件时没有错误.我做错了什么?
- 创建 OpenCV 非自由版本 v4.3 时出错,可折叠.cpp错误 C2039、2605
- 制作时出错,C++"db.c:2127:错误:在"||"标记之前应使用";"
- C++语法错误,编译器不会警告或 int v = func(&v) 出错;
- 在VS2015中访问类成员时运行时错误,但在Linux上未访问时出错
- 如何更改路径以修复错误"./main:加载共享库 libmkl_core.so 时出错?
- 为什么在定义类之前声明类的对象会在友元类中给出错误,而在友元函数中不会出错
- 安装 mply 库时出错.致命错误:gsl/gsl_sf.h:没有这样的文件或目录
- Rcpp/C++/R:比较日期时间矢量与日期时间时出错(错误:"运算符>"的不明确重载)
- 编译特征程序时出错:错误:'seq'不是'Eigen'的成员
- 可能的 GCC 链接器错误会导致将弱符号和局部符号链接在一起时出错
- 非标准语法;使用 '&' 创建指向成员的指针错误,将成员函数分配给向量时出错
- 运算符重载时出错(错误:"运算符<<不匹配(操作数类型为"std::basic_ostream<char>"和"const char [2]")
- 编译类 C++ 中的线程时出错(错误 xthread)
- 加载GLEW库时出错(错误LNK2001)
- 通过函数指针调用函数时出错(错误C2064)
- 编译core.obj时出错:错误LNK2001:未解析的外部符号_harmony_core_init
- 链接c++静态库与android ndk时出错(错误:无法识别文件格式)
- 使用GCC编译游戏时出错.(错误:将"Screen"的含义从"class Screen&q