gpt4 book ai didi

尝试从函数传递和返回一组指针时出现 C++ 错误

转载 作者:行者123 更新时间:2023-11-27 23:39:46 25 4
gpt4 key购买 nike

<分区>

我有两个(实际上是三个,但其中一个与此处无关)头文件,其中包含一个项目的类。一Node类包含一组指向其他节点的指针(它就像一棵树),另一个类是图形类(标题为 AML ),它包含一组指向各自层中节点的所有指针。

我想编写自己的 C++ 集合操作(​​因为我需要它们),所以我决定声明一个具有集合操作函数的命名空间。然而,当我尝试在我的 Node 中使用这些集合操作时类,我得到一个错误。相关代码如下:

aml.h

#pragma once
#include <set>
#include "Node.h"
#include "constant.h"

using namespace std;
class aml
{
// class stuff
};

namespace setops {
set<Node*> set_union(set<Node*> &a, set<Node*> &b);
// other set operations
}

aml.cpp

#include "aml.h"

using namespace std;


//class functions

set<Node*> setops::set_union(set<Node*> &a, set<Node*> &b) {
set<Node*> c;
for (Node* data : a) {
c.insert(data);
}
for (Node* data : b) {
c.insert(data);
}

return c;
}

Node.h

#pragma once
#include <string>
#include <set>
#include "aml.h"
#include "constant.h"

using namespace std;
class Node
{
private:
set<Node*> lower;
public:
set<Node*> getLower();
set<Node*> GL();
};

这里是 GL()发生错误的方法。这应该返回通过沿着树向下移动可以到达的所有节点的集合。 getLower()返回 lower包含节点的所有子节点的集合。

Node.cpp

#include "Node.h"

set<Node*> Node::getLower() {
return this->lower;
}

set<Node*> Node::GL() {
set<Node*> lowerSet;
lowerSet.insert(this);
if (this->lower.size() == 0) {
return lowerSet;
}
set<Node*> recurse;
for (Node* node : this->lower) {
recurse = node->GL();
lowerSet = setops::set_union(lowerSet, recurse); //This is the error line
}
return lowerSet;
}

我标记了问题行。它实际上有三个错误,一个在每个参数上,一个在等号上。错误说(它们都差不多,但这是参数一)

a reference of type "set::set<<error-type>*, std::less<<error-type>*>, std::allocator<<error-type>*>& "(non-const qualified) cannot be initialized with a value of type "set::set<Node*, std::less<Node*>, std::allocator<Node*>> "

我不知道发生了什么,感谢任何帮助!

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