【问题标题】:Can't find proper value to use without getting for insert function如果不获取插入功能,则无法找到要使用的正确值
【发布时间】:2015-02-25 06:57:08
【问题描述】:

我今天已经编码了几个小时,但似乎已经空白了。任务是创建一个购物清单并在其中添加某些项目,同时检查该项目是否已经在列表中,列表是否为空等等。

在处理作业时,我找不到要在我的插入函数中使用的正确值/变量,而且这么小的东西阻止了我做作业,这让我很生气。错误出现在我的 GroceryList.cpp 文件中,其中定义了所有函数并写出了所有代码,以便删除、插入和检查空列表。到目前为止,其他一切似乎都编译得很好(我仍然需要修复一些函数),但现在我正试图让我的插入函数工作。任何人都可以查看我的代码并指出我在插入函数中使用哪个值的正确方向吗?我什至正确地编写了插入函数吗?谢谢。

GroceryList.h

#ifndef GROCERYLIST_H
#define GROCERYLIST_H

#include <string>
using namespace std;

namespace CS151GroceryList
{

// node definition
struct ListNode
{
    string item;
    ListNode *link;
};

// define a type for pointers to nodes
typedef ListNode* ListNodePtr;

// define the GroceryList class
class GroceryList
{
public:

    // default constructor initializes an empty list
    GroceryList();

    // destructor - destroys the list and returns all memory to the heap
    ~GroceryList();

    // returns true if the list is empty; false otherwise
    bool empty();

    // check to see if an item is in the list.  If so, return true.  If not, return false.
    bool inList(const string& an_item);

    // prints all of the items in the list.  If insert is implemented correctly, the contents
    // of the list will be printed in alphabetical order with no repeats.
    void print();

    // if an_item is found, remove it from the list
    void remove(const string& an_item);

    // put a new item into the list.  The item should be placed into the correct
    // position in the list.  If the item is already in the list, no change is
    // made to the list.  Note, you should put A COPY of an_item into the list
    void insert(const string& an_item);


private:
    ListNodePtr top;
};


} // end CS151GroceryList namespace

#endif GROCERYLIST_H

GroceryListTest.cpp 测试功能以确保它们正常工作。将特定的杂货项目添加到列表中,吐出列表,然后删除列表中的内容。问题不在这里

#include <iostream>
#include <string>
#include "GroceryList.h"
using namespace std;
using namespace CS151GroceryList;

int main()
{
GroceryList mylist;

/**********************************************************************
* List insertion test                                                *
**********************************************************************/

cout << "Inserting items into the grocery list." << endl << endl;
mylist.insert("Eggs");
mylist.insert("Bananas");
mylist.insert("Wheat Bread");
mylist.insert("Peanut Butter");
mylist.insert("Milk");
mylist.insert("Apples");
mylist.insert("White Bread");

cout << "Finished inserting items." << endl;


cout << "Grocery list contents:" << endl;
mylist.print();

cout << endl << "Finished printing the list." << endl;

/**********************************************************************
* inList test                                                        *
**********************************************************************/

cout << "Testing in list functionality:" << endl;
if (mylist.inList("Oranges"))
{
    cout << "Oranges are on the list." << endl;
}
else
{
    cout << "Oranges are not on the list." << endl;
}
if (mylist.inList("Apples"))
{
    cout << "Apples are on the list." << endl;
}
else
{
    cout << "Apples are not on the list." << endl;
}
if (mylist.inList("Peanut Butter"))
{
    cout << "Peanut Butter is on the list." << endl;
}
else
{
    cout << "Peanut Butter is not on the list." << endl;
}
if (mylist.inList("White Bread"))
{
    cout << "White Bread is on the list." << endl;
}
else
{
    cout << "White Bread is not on the list." << endl;
}

/**********************************************************************
* List removal test                                                  *
**********************************************************************/
cout << "Testing list removal." << endl;
cout << endl << "I decided that I didn't want apples after all.  Removing apples." << endl;
mylist.remove("Apples");
cout << "After removing apples, grocery list contents:" << endl;
mylist.print();

cout << endl << "Finished printing the list." << endl << endl;

cout << endl << "I decided that I didn't want white bread either.  Removing white bread." << endl;
mylist.remove("White Bread");
cout << "After removing white bread, grocery list contents:" << endl;
mylist.print();

cout << endl << "Finished printing the list." << endl << endl;

cout << endl << "I'm lactose intolerant, I'd better not get milk.  Removing milk." << endl;
mylist.remove("Milk");
cout << "After removing milk, grocery list contents:" << endl;
mylist.print();

cout << endl << "Finished printing the list." << endl << endl;
}

GroceryList.cpp 错误在这个文件中

确切地说,错误出在这部分代码中。这是调用每个函数并进行所有插入、删除和检查的地方。我使用 an_item 作为占位符,以便我知道我需要在哪里找出函数的正确值。我已经尝试了我目前拥有的所有指针和其他值。我什至创建了一个临时值(此处未显示),但这似乎也不起作用。

void GroceryList::insert(const string& an_item)
{

    if (top == an_item.insert)
    {
        return;
    }

    char next;
    while (!empty())
    {
        delete(top);
    }

    if (an_item.insert == NULL)
    {
        top = NULL;
        return;
    }

    ListNodePtr temp = new ListNode;
    temp->item = an_item;
    temp->link = top;

    top = temp;
}

这是 GroceryList.cpp 的其余部分

#include <iostream>
#include <cstddef>
#include <string>
#include "GroceryList.h"

using namespace std;
using namespace CS151GroceryList;

GroceryList::GroceryList() : top(NULL)
{
    //left blank intentionally
 }
GroceryList::~GroceryList()
{
    while (!empty())
    {
        /*remove();*/
        delete top;
    }
}

bool GroceryList::empty()
{
    return(top == NULL);
}

void GroceryList::print()
{
    ListNode*temp = top;
    while (temp != top)
    {
        cout << temp->item << endl;
        temp = temp->link;
    }

    cout << temp->item << endl;
}

void GroceryList::remove(const string& an_item)
{
    if (empty())
    {
        cout << "Error: remove was attempted on empty list.\n";
        exit(1);
    }

}

void GroceryList::insert(const string& an_item)
{

    if (top == temp.insert)
    {
        return;
    }

    char next;
    while (!empty())
    {
        delete(top);
    }

    if (an_item.insert == NULL)
    {
        top = NULL;
        return;
    }

    ListNodePtr temp = new ListNode;
    temp->item = an_item;
    temp->link = top;

    top = temp;
}

bool GroceryList::inList(const string& an_item)
{
    ListNodePtr temp;
    temp = top;
    if (temp == an_item.insert)
    {
        temp = temp->link;
        if (temp != an_item.insert)
        {
            if (temp->link != temp->link)
            {
                return 1;
            }
        }
        /*cout << "Item is in list. Failed to insert." << endl;
        return 1;*/
    }

    else
    {
        cout << "Item is not in list. Inserting." << endl;
        return 0;
    }
}

这是我得到的错误:

error C3867:   'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::insert': function call missing argument list; use '&std::basic_string<char,std::char_traits<char>,std::allocator<char>>::insert' to create a pointer to member

我很确定这是因为我没有使用正确的值

【问题讨论】:

  • 当你使用if (an_item.insert == NULL)时你想做什么?
  • @RSahu 这应该是在尝试插入时检查列表中是否没有任何内容。
  • 不是答案,但 #endif GROCERYLIST_H 应该是 #endif //GROCERYLIST_H 并且 an_item.insert 应该是 an_item
  • @MohitJain 感谢您指出这一点。忘记评论了。谢谢!

标签: c++ list pointers linked-list


【解决方案1】:

你的代码中至少有这些问题:

1)

void GroceryList::insert(const string& an_item)
{
    if (top == temp.insert)
    {
        return;
    }

如果 temp 是局部变量,那么你不能在没有声明的情况下使用它(最好是正确初始化)。此外,“insert”是成员函数调用,你已经错过了大括号和参数列表,比如“temp.insert(smth)”

2)

if (an_item.insert == NULL)
{
    top = NULL;
    return;
}

同样的问题,您应该为成员调用插入提供参数。但是你不需要插入调用,因为 GroceryList::insert 的 an_item 参数是由 const 引用传递的,所以你可以在没有 NULL 检查的情况下使用它

【讨论】:

  • 我认为“ListNodePtr temp”定义了 temp
  • 你应该在使用前定义它
  • 哦,好吧。谢谢。我明天会在这里更新代码,因为我来晚了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-29
  • 2012-07-17
  • 1970-01-01
相关资源
最近更新 更多