简体   繁体   中英

Invalid conversion from 'const char*' to 'ItemType {aka char}' [-fpermissive]

I keep getting an error: invalid conversion from const char* to ItemType {aka char} [-fpermissive] I initially thought it was because I was using strings so I changed the typedef to strings and got a different error. I then changed the typedef back and am unable to understand this error does anyone know what I have done wrong? My errors or all for the insertion method.

This is my header file

#ifndef _LINKED_LIST
#define _LINKED_LIST
#include <string>
#include "Node.h"
#include "PrecondViolatedExcep.h"

typedef char ItemType;
class LinkedList
{
private:
   Node* headPtr;
   int itemCount;           // Current count of list items
   Node* getNodeAt(int position) const;
public:
   LinkedList();
   LinkedList(const LinkedList& aList);
   virtual ~LinkedList();
   bool isEmpty() const;
   int getLength() const;
   bool insert(int newPosition, const ItemType& newEntry);
   bool remove(int position);
   void clear();

   ItemType getEntry(int position) const throw(PrecondViolatedExcep);

   void setEntry(int position, const ItemType& newEntry) throw(PrecondViolatedExcep);
};

#endif

This is my main

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

int main()
{
    cout << "Emery Plummer" << endl;
    LinkedList* groceryList = new LinkedList();

    groceryList->insert(1, "Turkey");
    groceryList->insert(2, "Cheese");
    groceryList->insert(3, "Bread");
    groceryList->insert(4, "lettuce");
    groceryList->insert(5, "Tomatoes");
    groceryList->insert(1, "Soda");
    groceryList->insert(2, "nachos");

    int numberofItems = groceryList->getLength();
    cout << "My grocery list contains " <<  numberofItems << " number of things, theese are the items: "<< endl;
    for(int position = 1; position <= numberofItems; position++)
        cout << groceryList->getEntry(position) << "is first item" << position << endl;
    return0
}

This is my cpp

#include "LinkedList.h"
#include <cassert>

typedef char ItemType;
LinkedList::LinkedList() : headPtr(nullptr), itemCount(0)
{
}

LinkedList::LinkedList(const LinkedList& aList)
{
    itemCount = aList.itemCount;
    Node* origChainPtr = aList.headPtr;
    if(origChainPtr == nullptr)
        headPtr = nullptr;
    else
    {
        headPtr = new Node();
        headPtr -> setItem(origChainPtr-> getItem());
        Node* newChainPtr = headPtr;
        while(origChainPtr -> getNext()!= nullptr)
    {
        origChainPtr = origChainPtr -> getNext();
        Node nextItem = origChainPtr ->getItem();
        Node* newNodePtr = new Node(nextItem);
        newChainPtr -> setNext(newNodePtr);
        newChainPtr = newChainPtr -> getNext();
    }
    newChainPtr -> setNext(nullptr);
    }
}

LinkedList::~LinkedList()
{
   clear();
}  // end destructor


bool LinkedList::isEmpty() const
{
   return itemCount == 0;
}

int LinkedList::getLength() const
{
   return itemCount;
}

bool LinkedList::insert(int newPosition, const ItemType& newEntry)
{
   bool ableToInsert = (newPosition >= 1) && (newPosition <= itemCount + 1);
   if (ableToInsert)
   {
      Node* newNodePtr = new Node(newEntry);
      if (newPosition == 1)
      {
         newNodePtr->setNext(headPtr);
         headPtr = newNodePtr;
      }
      else
      {
         Node* prevPtr = getNodeAt(newPosition - 1);
         newNodePtr->setNext(prevPtr->getNext());
         prevPtr->setNext(newNodePtr);
      }
      itemCount++;
   }

   return ableToInsert;
}

bool LinkedList::remove(int position)
{
   bool ableToRemove = (position >= 1) && (position <= itemCount);
   if (ableToRemove)
   {
      Node* curPtr = nullptr;
      if (position == 1)
      {
         curPtr = headPtr; // Save pointer to node
         headPtr = headPtr->getNext();
      }
      else
      {
         Node* prevPtr = getNodeAt(position - 1);
         curPtr = prevPtr->getNext();
         prevPtr->setNext(curPtr->getNext());
      }
      curPtr->setNext(nullptr);
      delete curPtr;
      curPtr = nullptr;

      itemCount--;
   }

   return ableToRemove;
}

void LinkedList::clear()
{
   while (!isEmpty())
      remove(1);
}

ItemType LinkedList::getEntry(int position) const throw(PrecondViolatedExcep)
{
   bool ableToGet = (position >= 1) && (position <= itemCount);
   if (ableToGet)
   {
      Node* nodePtr = getNodeAt(position);
      return nodePtr->getItem();
   }
   else
   {
      string message = "getEntry() called with an empty list or ";
      message  = message + "invalid position.";
      throw(PrecondViolatedExcep(message));
   }
}

Node* LinkedList::getNodeAt(int position) const
{
   assert( (position >= 1) && (position <= itemCount) );
   Node* curPtr = headPtr;
   for (int skip = 1; skip < position; skip++)
      curPtr = curPtr->getNext();

   return curPtr;
}

Class LinkedList has this method:

bool insert(int newPosition, const ItemType& newEntry);

And in main.cpp you invoke this method this way:

groceryList->insert(1, "Turkey");

Now, "Turkey" is of type const char* while method insert demands second argument to be of type const ItemType& . You have defined ItemType as typedef char ItemType , so effectively insert demands const char& . As you know const char* (a pointer ) is something different than const char& (a reference ). You have to either change declaration and definition of insert or pass "Turkey" as reference, not as a pointer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM