C++ 将char指针作为模板参数传递给标头上的函数

C++ 将char指针作为模板参数传递给标头上的函数,c++,C++,我试图模拟添加/删除课程,但我得到了分割错误。 我还尝试了char数组,但仍然有问题,我不擅长C++,我搜索模板的使用,每个例子都像地雷,但是我的工作不好…… 头文件: template<class T> class LL { protected: node<T> *head, *last; int count; public: LL(); ~LL(); bool emptyList(); int length(){retu

我试图模拟添加/删除课程,但我得到了分割错误。 我还尝试了char数组,但仍然有问题,我不擅长C++,我搜索模板的使用,每个例子都像地雷,但是我的工作不好…… 头文件:

template<class T>
class LL
{
protected:
    node<T> *head, *last;
    int count;
public:
    LL();
    ~LL();
    bool emptyList();
    int length(){return count;};
    T back();
    T front();
    void destroyList();
    node<T> *search(T&);
    void insertFirst(T&);
    void insertLast(T&);
    void deleteNode(T&);
    void printList();
        template<class T>
        void LL<T>::insertLast(T& item)
        {
            node<T> *p = new node<T>;
            p->info = item;
            p->link = NULL;
            if(head != NULL)
            {
                last->link = p;
                last = p;
            }else head = last = p;
            count++;
        }
    template<class T>
    node<T> *LL<T>::search(T& item)
    {
        bool found = false;
        node<T> *p = head;
        while((p != NULL) &&  found)
        {
            if(p->info == item)
                found = true;
            else p = p->link;
        }
        return p;
    }
模板
LL班
{
受保护的:
节点*头,*尾;
整数计数;
公众:
LL();
~LL();
bool-emptyList();
int length(){return count;};
T back();
T前面();
作废销毁清单();
节点*搜索(T&);
无效插入第一个(T&);
空心镶片(T&);
无效删除节点(T&);
作废打印列表();
模板
void LL::insertLast(T和item)
{
node*p=新节点;
p->info=物料;
p->link=NULL;
if(head!=NULL)
{
最后->链接=p;
last=p;
}else head=last=p;
计数++;
}
模板
节点*LL::搜索(T&item)
{
bool-found=false;
节点*p=头部;
while((p!=NULL)&&found)
{
如果(p->info==项目)
发现=真;
否则p=p->link;
}
返回p;
}
主类

 LL<char*> ll;
    while(true)
    {
        char command[256];
        cout << "Enter Add | Drop | Change | List | Quit:";
        cin>>command;
        cout<<endl;

        if(strcmp(command,"Add")==0)
        {
            char* course;
            char* credit;
            char* section;

            cout<<"Enter CourseCode, Credit and Section to be added:"<<endl;
            cin>>course>>credit>>section;
            cout<<course<<" "<<credit<<" "<<section<<endl;

            if(ll.search(course) == NULL)
            {
                ll.insertLast(course);
            }
        }
}`
LL;
while(true)
{
char命令[256];
cout>命令;
coutsection;
这不管用

strcmp(command,"Add")==0
因为
int strcmp(const char*str1,const char*str2);
“Add”
是一个字符串。您最好像

std::string str("Add");
char* chr = (char*)str.c_str();
strcmp(command, chr)==0

您的代码存在多个问题。最基本的问题是您没有为通过
std::cin
写入的
char*
指针分配内存。如果通过

char* course = new char[256];
segfault是固定的。然后,
LL
可以用作链表类型——不过最好还是使用
std::string


此外,您的
LL::search
方法已被破坏,您需要重新考虑其逻辑。除此之外,将代码复制粘贴到问题框中可能会产生一些更为琐碎的问题。

您确实应该使用
std::string
来处理字符串。您的
节点
实现是什么?还要注意t您可以使用
std::list
@NathanOliver我试了两个小时,字符串是正确的答案,非常感谢这是不正确的。像“Add”这样的字符串文字可以转换为std::string,但首先被解释为r值
char
数组,例如
const char[]
。是的,我的搜索和插入不太好,因为我将所有属性作为凹面字符串插入到一起;它类似于Ceng553 3 1=>这意味着课程代码Ceng553,课程学分为3,节为1,但这破坏了我的搜索算法,因为我必须只使用课程代码进行搜索,看起来我必须使用字符串a进行添加rray@hkn好的,在你展示的代码中没有连接,所以我假设你以后添加了这个。我指的问题是你可能想把
notfound
放在while条件中,而不是
found
。是的,稍后再放,如果它是java,我可能会创建一个内部类来保存课程代码、学分和章节看起来我需要这样做,我将创建一个结构并插入它