これは単純なものであるべきだと思いますが、同じ抽象クラスから継承するさまざまなクラスのリンクリストを作成できない理由を理解しようと多くの時間を費やしました
リンク リストの先頭にプッシュしている各クラス (つまり、BadCruisingShootingAgent) は、抽象クラス Agent から継承しています。
エラー: フィールド 'Node::data' を抽象型 'Agent' として宣言できません
私のmain.cppファイルは次のとおりです。
int main()
{
LinkedList<Agent> *agentList= new LinkedList<Agent>();
agentList->push_front(*(new BadCruisingShootingAgent));
agentList->push_front(*(new BadFollowingDisarmedAgent));
agentList->push_front(*(new BadFollowingShootingAgent));
agentList->push_front(*(new BadStationaryDisarmedAgent));
agentList->push_front(*(new BadStationaryShootingAgent));
agentList->push_front(*(new GoodCruisingDisarmedAgent));
agentList->push_front(*(new GoodCruisingShootingAgent));
agentList->push_front(*(new GoodFollowingDisarmedAgent));
agentList->push_front(*(new GoodFollowingShootingAgent));
agentList->push_front(*(new GoodStationaryDisarmedAgent));
agentList->push_front(*(new GoodStationaryShootingAgent));
for(int i=0; i<agentList->size(); i++)
{
cout << agentList->at(i).getType()<<" "<<agentList->at(i).nextMovingDirection(10,10)<<" "<<agentList->at(i).shootingDirection(10,10)<<endl;
}
return(0);
}
手動で書くだけなら問題はないのに、なぜこれが機能しないのかわかりません。
Agent *a= new BadCruisingShootingAgent;
cout << a->getType()<<" "<<a->extMovingDirection(10,10)<<" "<<a->shootingDirection(10,10)<<endl;
次に、リンクされたリストのクラス関数 push_front は次のように定義されます。
template <typename T>
void LinkedList<T>::push_front(const T& val)
{
//make a new node
Node<T>* newOne = new Node<T>(val);
//push it onto the front of the list
newOne->next = this->head;
this->head = newOne;
//increase the length of the list by one
this->length++;
}
私のノードクラスは次のように定義されています:
template <typename T>
class Node
{
public:
Node(const T& d);
T data;
Node<T>* next;
};
template <typename T>
Node<T>::Node(const T& d)
: data(d)
{
this->next = NULL;
}