如何解决如何将模板与链表实现一起使用?
我刚开始使用模板,却遇到一个问题,就是我不断遇到细分错误。我将问题缩小到Node结构中的数据是模板这一事实,并且当我设置该数据时,它不起作用。我通过将数据设置为int进行了测试,并以此方式进行了手动设置并使其正常工作,但我希望将其作为模板。
#include <fstream>
#include <cstring>
#include <sstream>
template<class T>
class Game // made a class so it was easier to keep track of the head pointer
{
private:
struct Node
{
T cmdName; // command name
T cmdDesc; // command description
struct Node* pNext;
};
struct Node* pHead = NULL;
public:
Game()
{
T lineTemp,tempName,tempDesc;
std::ifstream dataFile;
dataFile.open("commands.csv");
while(std::getline(dataFile,lineTemp))
{
std::getline(dataFile,lineTemp);
std::istringstream ss(lineTemp); // using string stream to split the string pulled in from the csv file (idk how else to do this)
std::getline(ss,',');
std::getline(ss,tempDesc,');
insert_data_front(tempName,tempDesc);
}
}
// all functions and their definitions will be here because you shouldnt split the files for template functions (i think?)
void insert_data_front(T cmdName_c,T cmdDesc_c)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); // returns a pointer the same size as the data block needed
newNode->cmdName = cmdName_c; // < --- HERES THE ISSUE
newNode->cmdDesc = cmdDesc_c;
newNode->pNext = pHead;
pHead = newNode;
}
};
问题出在函数insert_data_front
上,我将如何处理?谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。