I'm trying to create a linked list that stores a pointer to a binary tree,
The binary tree class is a subclass derived from a generic TreeNode
class which I made.
The TreeNode
class has it's AddNode
method implemented (just as a dummy, but it is should be callable), but when I try to invoke that method from a subclass of TreeNode
I am getting the following error:
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')
Here is the relevant part of the 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;
}
Here is 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();
};
Here is the linked list, called 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);
};
And here is the function where the error occurs:
// 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;
}
I tried overriding the AddNode
method inside ArtistPlaysNode
, but that didn't work and made the compiler complain about being unable to cast from one pointer to the other.
Trying to search online for an answer didn't bring up any relevant results