无法使用 'std::shared_ptr_access' 类型的表达式初始化 'TreeNode' 类型的参数'
Cannot initialize a parameter of type 'TreeNode' with an expression of type 'std::shared_ptr_access''
我正在尝试创建一个链表来存储指向二叉树的指针,
二叉树 class 是一个子 class 派生自我制作的泛型 TreeNode
class.
TreeNode
class 实现了它的 AddNode
方法(就像一个虚拟对象,但它应该是可调用的),但是当我尝试从子程序调用该方法时class of 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
class 的相关部分:
// 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
方法,但这没有用,编译器抱怨无法从一个指针转换为另一个指针。
尝试在线搜索答案未找到任何相关结果
好吧,简而言之,错误是由于缺少 Forward Declarations。
请注意,ArtistPlaysNode
class 有一个 MostPlayedListNode
类型和 SongPlaysNode
类型的 shared_ptr 作为其成员。
同时,MostPlayedList
class 有一个 shared_ptr 类型 'ArtistPlaysNode' 和类型 SongPlaysNode
作为它的成员。
此外,ArtistPlaysNode
和SongPlaysNode
都是从TreeNode
class派生出来的。
这创建了一个场景,其中这些 class 以几乎循环的方式拥有其他类型的成员。
这通常会导致以下类型的错误:
expected class name before '{' token.
如 this question
所示
或者可能会导致以下类型的错误:
'NAME' was not declared in this scope
如 enter link description here
所示
为了解决这个问题,我们需要或者确保class、函数或header依赖的所有东西在它被声明之前声明正在使用。
或我们需要为编译器提供forward-declarations,这将允许编译器识别class,而无需其完整定义可用.
就我的代码而言,修复是在 MostPlayedListNode
、SongPlaysNode
和 ArtistPlaysNode
的 class 文件中添加前向声明。
例如,更新后的 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:
总而言之,在编写某些数据结构时,forward-declarations对于编译器识别所有必需的object很重要,如果当引用它们的 object 需要它们时,它们还没有定义。
在我的代码中,我需要 forward-declarations 来解释 Mutual Recursion,但情况可能并非总是如此。
我正在尝试创建一个链表来存储指向二叉树的指针,
二叉树 class 是一个子 class 派生自我制作的泛型 TreeNode
class.
TreeNode
class 实现了它的 AddNode
方法(就像一个虚拟对象,但它应该是可调用的),但是当我尝试从子程序调用该方法时class of 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
class 的相关部分:
// 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
方法,但这没有用,编译器抱怨无法从一个指针转换为另一个指针。
尝试在线搜索答案未找到任何相关结果
好吧,简而言之,错误是由于缺少 Forward Declarations。
请注意,ArtistPlaysNode
class 有一个 MostPlayedListNode
类型和 SongPlaysNode
类型的 shared_ptr 作为其成员。
同时,MostPlayedList
class 有一个 shared_ptr 类型 'ArtistPlaysNode' 和类型 SongPlaysNode
作为它的成员。
此外,ArtistPlaysNode
和SongPlaysNode
都是从TreeNode
class派生出来的。
这创建了一个场景,其中这些 class 以几乎循环的方式拥有其他类型的成员。
这通常会导致以下类型的错误:
expected class name before '{' token.
如 this question
或者可能会导致以下类型的错误:
'NAME' was not declared in this scope
如 enter link description here
为了解决这个问题,我们需要或者确保class、函数或header依赖的所有东西在它被声明之前声明正在使用。
或我们需要为编译器提供forward-declarations,这将允许编译器识别class,而无需其完整定义可用.
就我的代码而言,修复是在 MostPlayedListNode
、SongPlaysNode
和 ArtistPlaysNode
的 class 文件中添加前向声明。
例如,更新后的 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:
总而言之,在编写某些数据结构时,forward-declarations对于编译器识别所有必需的object很重要,如果当引用它们的 object 需要它们时,它们还没有定义。 在我的代码中,我需要 forward-declarations 来解释 Mutual Recursion,但情况可能并非总是如此。