从类中访问结构和实现图形的其他问题
C++ Access to struct from class and other problems with implementing graph
我正在使用DLL实现图形程序。我不知道如何从我的类访问结构没有错误。也许我的代码不好,或者是类/结构声明有问题。
代码:#include <iostream>
#include <vector>
#include <set>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#include <climits>
#include <algorithm>
using namespace std;
typedef struct Node Node;
typedef struct Edge Edge;
typedef struct DLList DLList;
struct Node
{
int id;
int dist;
Node* prev;
vector<Edge*> edges;
};
struct Edge
{
int length;
Node* to;
};
struct DLList
{
DLList *next;
DLList *prev;
Node* val;
};
class DList
{
public:
DLList* first = 0;
DLList* last = 0;
Dlist()
{
}
Node* getFirst()
{
if(first == 0)
{
cout << "Pusta lista " << endl;
}
return first->val;
}
DLList* add(Node* n)
{
DLList* newNode = new DLList;
if(first == 0)
{
newNode->val = n;
newNode->next = 0;
newNode->prev=0;
first = newNode;
last = newNode;
}
else
{
newNode->val = n;
newNode->next = 0;
newNode->prev=last;
last = newNode;
last->next=newNode;
}
}
void removeFirst()
{
if(first!=0)
{
first = first->next;
if(first != 0)
{
first->prev = 0;
}
}
}
void remove(DLList* e)
{
if(e==first)
{
removeFirst();
}
else if(e==last)
{
last = last->prev;
last -> next = 0;
}
else
{
e->prev->next = e->next;
e->next->prev = e->prev;
}
}
void remove(Node* n)
{
if(n==first->val)
{
removeFirst();
}
else if(n==last->val)
{
last = last->prev;
last->next = 0;
}
else
{
for(DLList* i=first; i!=0; i=i->next)
{
if(n==i->val)
{
i->prev->next = i->next;
i->next->prev = i->prev;
break;
}
}
}
}
bool isEmpty()
{
return first == 0;
}
};
vector<Node*> nodes_main;
int n_edges, n_nodes, maxlen;
Node* start=0;
Node* end=0;
const int infinity = 1<<28;
void dial()
{
vector<DList> buckets(maxlen*nodes_main.size()+1);
vector<DLList*> arr(nodes_main.size());
int i;
for(i=0; i<buckets.size(); i++)
{
DList listaa;
buckets[i] = listaa;
}
for(Node* n : nodes_main)
{
if(n==start)
{
continue;
}
n->dist = infinity;
n->prev = 0;
}
start->dist = 0;
start->prev = 0;
arr[start->id] = buckets[0].add(start);
int j = 0;
int z=0;
for(z=0;z<nodes_main.size();z++)
{
while(buckets[j].empty())
{
j++;
if(j >= buckets.size())
{
return;
}
}
Node* v = buckets[j].getFirst();
buckets[j].removeFirst();
arr[v->id]=0;
for(Edge* e : v->edges)
{
if(v->dist + e->length < e->to->dist)
{
if(e->to->dist < infinity)
{
buckets[e->to->dist].remove(arr[e->to->id]);
}
e->to->dist = v->dist + e->length;
e->to->prev = v;
arr[e->to->id]=buckets[e->to->dist].add(e->to);
}
}
}
}
int main()
{
Node* newNode = new Node;
int i=0,j=0,from=0;
maxlen=0;
n_nodes=5;
n_edges=18;
for(i=0; i<n_nodes;i++)
{
newNode->id = n_nodes;
nodes_main[n_nodes] = newNode;
}
Edge* newEdge = new Edge;
newEdge->length=5;
newEdge->to=nodes_main[4];
nodes_main[0]->edges.push_back(newEdge);
maxlen = 5;
return 0;
}
错误:dial.cpp:43:18: sorry, unimplemented: non-static data member initializers
dial.cpp:43:18: error: ‘constexpr’ needed for in-class initialization of static data member ‘first’ of non-integral type
dial.cpp:44:17: sorry, unimplemented: non-static data member initializers
dial.cpp:44:17: error: ‘constexpr’ needed for in-class initialization of static data member ‘last’ of non-integral type
dial.cpp:45:8: error: ISO C++ forbids declaration of ‘Dlist’ with no type [-fpermissive]
dial.cpp: In member function ‘Node* DList::getFirst()’:
dial.cpp:52:6: error: ‘first’ was not declared in this scope
dial.cpp:56:10: error: ‘first’ was not declared in this scope
dial.cpp: In member function ‘DLList* DList::add(Node*)’:
dial.cpp:62:6: error: ‘first’ was not declared in this scope
dial.cpp:68:4: error: ‘last’ was not declared in this scope
dial.cpp:74:18: error: ‘last’ was not declared in this scope
dial.cpp: In member function ‘void DList::removeFirst()’:
dial.cpp:82:6: error: ‘first’ was not declared in this scope
dial.cpp: In member function ‘void DList::remove(DLList*)’:
dial.cpp:95:9: error: ‘first’ was not declared in this scope
dial.cpp:99:14: error: ‘last’ was not declared in this scope
dial.cpp: In member function ‘void DList::remove(Node*)’:
dial.cpp:113:9: error: ‘first’ was not declared in this scope
dial.cpp:117:14: error: ‘last’ was not declared in this scope
dial.cpp: In member function ‘bool DList::isEmpty()’:
dial.cpp:138:10: error: ‘first’ was not declared in this scope
dial.cpp: In function ‘void dial()’:
dial.cpp:178:29: error: ‘class DList’ has no member named ‘empty’
我删除了相同主题的上一篇文章(所有代码都在这里)。请帮助如何做:)
第43行:将DLList* first = 0;
更改为DLList *first;
,并在所有构造函数中将first
初始化为0
。
第44行:类似
第45行:将Dlist()
更改为DList()
(如果它意味着是构造函数)或void Dlist()
,否则
第178行:假定empty
应该是isEmpty
。
其他错误是这些错误的级联。我也不知道你为什么把你的错误粘贴两次。
DLList *first = 0;
只支持c++ 11,显然你的编译器不支持
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- C++图形类指针混淆
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- 在其他文件中创建类时在 c++ 项目中不起作用
- 类与私有变量的其他类之间的线程安全性
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 如何在内核C++中使用1920x1080x16M图形或类似的16M颜色?(VGA)
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- GlobalAlloc而不是其他分配方法
- C++从其他 constexpr 创建 lambda 不能按顺序执行 Constexpr
- 断言中的Fold表达式在某些计算机上编译,但在其他计算机上不编译
- 从其他线程更新QT图形视图
- C++:图形 ADT 应该有一个顶点列表和一个边列表,或者只是带有指向其他顶点的指针的顶点
- facebook图形API使用libcurl/其他库
- 使用SDL_ttf绘制文本,同时使用其他图形
- 显示条形图(和其他时髦的图形)
- 从类中访问结构和实现图形的其他问题