无法使用类型为"std::shared_ptr_access"的表达式初始化类型为"树节点"的参数



我正在尝试创建一个链表来存储指向二叉树的指针, 二叉树类是从我创建的泛型TreeNode类派生的子类。

TreeNode类实现了它AddNode方法(就像一个虚拟的,但它应该是可调用的),但是当我尝试从TreeNode的子类调用该方法时,我收到以下错误:

Cannot initialize object parameter of type 'TreeNode' with an expression of type: 'std::__shared_ptr_access<ArtistPlaysNode>,__gnu_cxx::_S_atomic, false, false>::element_type'(aka 'ArtistPlaysNode')

以下是TreeNode类的相关部分:

// TreeNode.h
class TreeNode {
protected:
int key;
int height;
shared_ptr<TreeNode> father;
shared_ptr<TreeNode> left;
shared_ptr<TreeNode> right;
public:
explicit TreeNode(int key);
TreeNode(int key, shared_ptr<TreeNode> father, shared_ptr<TreeNode> left, shared_ptr<TreeNode> right);
virtual StatusType AddNode(shared_ptr<TreeNode> node);
};
// TreeNode.cpp
StatusType TreeNode::AddNode(shared_ptr<TreeNode> node) {
return INVALID_INPUT;
}

这是ArtistPlaysNode

// ArtistPlaysNode.h
class ArtistPlaysNode : public TreeNode {
private:
int artistId;
shared_ptr<SongPlaysNode> SongPlaysTree;
shared_ptr<MostPlayedListNode> ptrToListNode;
public:
ArtistPlaysNode(int artistId);
ArtistPlaysNode(int artistId, shared_ptr<SongPlaysNode> ptrToSongPlaysTree, shared_ptr<MostPlayedListNode> ptrToListNode);
int GetArtistId();
};

这是链表,称为MostPlayedListNode

// MostPlayedListNode.h
class MostPlayedListNode {
private:
int numberOfPlays;
shared_ptr<ArtistPlaysNode> artistPlaysTree;
shared_ptr<ArtistPlaysNode> ptrToLowestArtistId;
shared_ptr<SongPlaysNode> ptrToLowestSongId;
shared_ptr<MostPlayedListNode> previous;
shared_ptr<MostPlayedListNode> next;
public:
// Create the first node in the list (0 plays)
MostPlayedListNode(int numOfPlays);
// Create a new node with a new highest number of plays
MostPlayedListNode(int numOfPlays, shared_ptr<MostPlayedListNode> previous);
// Create a new node with a number of plays between to values (1<2<3)
MostPlayedListNode(int numOfPlays, shared_ptr<MostPlayedListNode> previous, shared_ptr<MostPlayedListNode> next);
bool AddArtist(shared_ptr<ArtistPlaysNode> artistNode);
};

这是发生错误的函数:

// MostPlayedListNode.cpp
bool MostPlayedListNode::AddArtist(shared_ptr<ArtistPlaysNode> artistNode) {
if (ptrToLowestArtistId) {
// There are already artists stored in this linked list
this->artistPlaysTree->AddNode(artistNode); // -->>> this line throws the error.
return true
} else {
this->artistPlaysTree = artistNode;
return true;
}
return false;
}

我尝试在ArtistPlaysNode中覆盖AddNode方法,但这不起作用,并使编译器抱怨无法从一个指针转换为另一个指针。

尝试在线搜索答案没有显示任何相关结果

好吧,简而言之,错误是由缺少前向声明引起的。

请注意,ArtistPlaysNode类具有MostPlayedListNodeSongPlaysNode类型的shared_ptr作为其成员。 同时,MostPlayedList类具有"ArtistPlaysNode"类型的shared_ptr,并且类型为SongPlaysNode作为其成员。

此外,ArtistPlaysNodeSongPlaysNode都派生自TreeNode类。

这创造了一个场景,其中这些类以几乎循环的方式具有其他类型的成员。

这通常会导致以下类型的错误:

expected class name before '{' token.如本问题所示

或者它可能会导致以下类型的错误:

'NAME' was not declared in this scope如在此处输入链接说明所示

为了解决这个问题,我们需要确保在使用类、函数或标头

之前声明所有内容。或者我们需要为编译器提供前向声明,这些声明将允许编译器识别该类,而无需提供其完整的定义。

就我的代码而言,修复是在MostPlayedListNodeSongPlaysNodeArtistPlaysNode的类文件中添加前向声明。

例如,更新的MostPlayedListNode.h文件的顶部:

using std::shared_ptr;
using std::make_shared;
class ArtistPlaysNode; // this is a forward declaration
class SongPlaysNode; // this is a forward declaration
class MostPlayedListNode {
private:
int numberOfPlays;
shared_ptr<ArtistPlaysNode> artistPlaysTree;
shared_ptr<ArtistPlaysNode> ptrToLowestArtistId;
shared_ptr<SongPlaysNode> ptrToLowestSongId;
shared_ptr<MostPlayedListNode> previous;
shared_ptr<MostPlayedListNode> next;
public:

以及更新的ArtistPlayesNode.h文件:

using std::shared_ptr;
using std::make_shared;
class SongPlaysNode; // this is a forward declaration
class MostPlayedListNode; // this is a forward declaration
class ArtistPlaysNode : public TreeNode {
private:
int artistId;
shared_ptr<SongPlaysNode> SongPlaysTree;
shared_ptr<MostPlayedListNode> ptrToListNode;
public:

总之,在编写某些数据结构时,前向声明对于编译器识别所有必要的对象非常重要,如果引用它们的对象在需要它们时尚未定义它们。 在我的代码中,我需要前向声明来解释相互递归,但情况可能并非总是如此。

最新更新