gpt4 book ai didi

c++ - C++ 中预期的嵌套名称说明符错误

转载 作者:行者123 更新时间:2023-11-30 03:08:19 25 4
gpt4 key购买 nike

我编写了一些类来实现树 ADT,它存储两组边和一组节点。它存储它们的是 SSet 类,这是使用 set 库的接口(interface)。

类的大纲和SSet.H是工作人员给我们的,我写了函数。

第 103 行(函数 GenGraphTemp::RemoveEdge)编译器 (g++) 告诉:

expected nested-name-specifier before NodeEdge
expected ',' or '...' before '&' token
ISO C++ forbids declaration of 'NodeEdge' with no type

同样对于 RemoveEdge 之后的函数,它说:

预期';'在第 106 行的“const”之前

预期';'在“SSet”expected ';' 之前在第 120 行的“const” 之前

预期';'在第 134 行的“ friend ” 之前

#include "sset.H"

using namespace std;

#define EQ(a1,a2) (!(a1 < a2) && !(a2 < a1))
#define MAX(a1,a2) ((a2<a1)?a1:a2)
#define MIN(a1,a2) ((a1<a2)?a1:a2)

template <class Node>
class Edge {
public:
...

protected:
Node _start;
Node _end;
};


template <class Node>
class GenGraphTemp {
public:

// Adds a new node to the nodes set
bool AddNode (const Node& n1){return _nodes.Add(n1);}

// Removes a node from the nodes set and all edges connected to it
bool RemoveNode (const Node& n1) {
int edges_size = _edges.Size();
int i;
Edge const *epointer;
//remove edges connected to n1
for (i=0;i<edges_size;i++){
(((i==0)? *epointer=_edges.GetFirst(): *epointer=_edges.GetNext()));
if (EQ(epointer->GetStart(),n1)||EQ(epointer->GetEnd(),n1)){
(_edges.Remove(*epointer)==false) && (return false);
}
}
// remove node
return _nodes.Remove(n1);
}

// Add a new edge to the edges set
bool AddEdge (const Node& n1, const Node& n2) {

(EQ(n1,n2)) && (return false);

if ((_nodes.IsIn(n1)) && (_nodes.IsIn(n2))){
typename NodeEdge edge(n1,n2);
return _edges.Add(edge);
}
return false;
}

// Removes an edge from edges set
bool RemoveEdge (const typename NodeEdge& e1) {return _edges.Remove(e1);}

// Check if two nodes are connected
bool RConnected const (const Node& n1, const Node& n2) {
Edge const *epointer;
int edges_size = _edges.Size();
int i;
for (i=0; i<edges_size; i++){
((i==0)? *epointer=_edges.GetFirst(): *epointer=_edges.GetNext());
if (EQ(epointer->GetStart(),MIN(n1,n2)) && EQ(epointer->GetEnd(),MAX(n1,n2))){
return true;
}
}
}

// Return a set of all the nodes that are connected to the input node through an edge
// if the node does not have ant neighbours, the function will return an empty set
SSet<Node>* Neighbours const (const Node& n1) {
Edge const *epointer;
int i, edge_size;
SSet<Node>* neighbours = new SSet<Node>;
edge_size=_edges.Size();
for (i=0; i<edges_size; i++){
((i==0)? *epointer=_edges.GetFirst(): *epointer=_edges.GetNext());
if (EQ(epointer->GetStart(),n1)||EQ(epointer->GetEnd(),n1)){
(neighbours->IsIn(*epointer)) || (neighbours->Add(*epointer);
}
}
return neighbours;
}

friend ostream& operator<<(ostream& os, GenGraphTemp<Node>& gr)
{
os << "Nodes:\n" << gr._nodes << "Edges:\n" << gr._edges;
return os;
}


protected:
typedef Edge<Node> NodeEdge;
SSet<Node> _nodes;
SSet<NodeEdge> _edges;
};

我不明白哪里出了问题,出了什么问题。

编辑:

我已经删除了 Edge 类的函数和运算符的实现以减少行数。我没有删除 GenGraphTemp 类的实现,因为我不知道问题出在其中一个还是其他地方。

我还尝试用备注中的每个“好”函数编译代码,但没有帮助。

最佳答案

// Removes an edge from edges set
bool RemoveEdge (const typename NodeEdge& e1) {return _edges.Remove(e1);}

// Check if two nodes are connected
bool RConnected const (const Node& n1, const Node& n2) {

制作

// Removes an edge from edges set
bool RemoveEdge (const NodeEdge& e1) {return _edges.Remove(e1);}

// Check if two nodes are connected
bool RConnected (const Node& n1, const Node& n2) const {

等....

关于c++ - C++ 中预期的嵌套名称说明符错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5134641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com