Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;函数返回一个链表_C++_Templates_Input_Linked List - Fatal编程技术网

C++ C++;函数返回一个链表

C++ C++;函数返回一个链表,c++,templates,input,linked-list,C++,Templates,Input,Linked List,我被要求做的是: Q) 一个句子由一系列以句号结尾的字符组成。编写一个函数,返回一个字符链接列表,用户在其中键入字符并将其添加到列表中。返回的列表应包括句号。 应称之为: LinkedList<char> *sentence; sentence = setUpSentence(); LinkedList*语句; 句子=句子(); 我曾经尝试过写这篇文章,但我很挣扎,因为这是我第一次使用链表和类似的函数 主文件 #include "LinkedList.h" #includ

我被要求做的是: Q) 一个句子由一系列以句号结尾的字符组成。编写一个函数,返回一个字符链接列表,用户在其中键入字符并将其添加到列表中。返回的列表应包括句号。 应称之为:

LinkedList<char> *sentence;
sentence = setUpSentence();
LinkedList*语句;
句子=句子();
我曾经尝试过写这篇文章,但我很挣扎,因为这是我第一次使用链表和类似的函数

主文件

#include "LinkedList.h"
    #include "ListNode.h"
    #include "Node.h"
    #include <iostream>
    #include <stdlib.h>
    using namespace std;

    LinkedList<char> *setUpSentence() {
        //allocate the linked list objet
        LinkedList<char> *sentence = new LinkedList<char>();
        char ch;
        do {
            cout << "Enter characters to add, enter full stop to finish adding." << endl;
            ch = cin.get();
            sentence->addAtEnd(ch);
        } while (ch != '.');

        return sentence;
    }

    int main() {

        //call the function, store the returned pointer in sentence variable
        LinkedList<char> *sentence = setUpSentence();
        //working with the linked list
        sentence = setUpSentence();



        cout << sentence->getAtFront() << endl;

//delete to avoid memory leak
        delete sentence;
    }
#包括“LinkedList.h”
#包括“ListNode.h”
#包括“Node.h”
#包括
#包括
使用名称空间std;
LinkedList*设置语句(){
//分配链表对象
LinkedList*语句=新建LinkedList();
char ch;
做{
cout(0)
{
电流=第一;
首先返回->项目;
}
否则返回NULL;
}
编辑: LinkedList.h文件中的addAtFront方法

template <typename T>
void LinkedList<T>::addAtFront(T item)
{
    ListNode<T> *l = new ListNode<T>(item, NULL, first);
    first = l;
    if (last == NULL)
        last = l;
    size = 1;
}
模板
无效链接列表::addAtFront(T项)
{
ListNode*l=新ListNode(项,空,第一);
第一个=l;
if(last==NULL)
last=l;
尺寸=1;
}

我不知道您为什么试图在return语句中递归调用
setup句子()

LinkedList<char> * setUpSentence() {
    // make sure to allocate the linked list object
    LinkedList<char> *sentence = new LinkedList<char>();
    char ch;
    do {
        cout << "Enter characters to add, enter full stop to finish adding." << endl;
        ch = cin.get();
        sentence->addAtEnd(ch);
    } while (ch != '.');

    return sentence;
}

int main() {
    // calling the function, store the returned pointer in sentence variable
    LinkedList *sentence = setUpSentence();

    // ... working with the linked list ...

    delete sentence;  // don't forget to delete it to avoid memory leak!
}
LinkedList*设置句子(){
//确保分配链表对象
LinkedList*语句=新建LinkedList();
char ch;
做{

这个递归调用显然是有问题的,尤其是考虑到结果是完全未使用的。其次,您没有提供所有相关的代码,这让我们不得不猜测
addAtFront
是否正确实现了。@whozCraig我的代码更新了alex Petrenko的建议&包括addAtFront方法。您不能删除
语句
然后稍后使用
cout
语句
。现在代码有问题吗?我认为原始错误不再适用于更改的代码。您不应该调用
setup句子()
两次。第一次调用会泄漏内存。
LinkedList<char> * setUpSentence() {
    // make sure to allocate the linked list object
    LinkedList<char> *sentence = new LinkedList<char>();
    char ch;
    do {
        cout << "Enter characters to add, enter full stop to finish adding." << endl;
        ch = cin.get();
        sentence->addAtEnd(ch);
    } while (ch != '.');

    return sentence;
}

int main() {
    // calling the function, store the returned pointer in sentence variable
    LinkedList *sentence = setUpSentence();

    // ... working with the linked list ...

    delete sentence;  // don't forget to delete it to avoid memory leak!
}