从类中访问结构和实现图形的其他问题

C++ Access to struct from class and other problems with implementing graph

本文关键字:图形 其他 问题 实现 访问 结构      更新时间:2023-10-16

我正在使用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,显然你的编译器不支持