【问题标题】:Invalid conversion from 'const char*' to 'ItemType {aka char}' [-fpermissive]从 'const char*' 到 'ItemType {aka char}' 的无效转换 [-fpermissive]
【发布时间】:2014-04-11 13:13:21
【问题描述】:

我不断收到一个错误:从const char*ItemType {aka char} [-fpermissive] 的无效转换我最初认为这是因为我使用的是字符串,所以我将 typedef 更改为字符串并得到了不同的错误。然后我将 typedef 改回来,无法理解这个错误,有人知道我做错了什么吗? 我的错误或全部是插入方法。

这是我的头文件

#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

这是我的主线

#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
}

这是我的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;
}

【问题讨论】:

    标签: c++ pointers reference type-conversion typedef


    【解决方案1】:

    类LinkedList有这个方法:

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

    main.cpp 中,您以这种方式调用此方法:

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

    现在,“Turkey”的类型为 const char*,而方法 insert 要求第二个参数的类型为 const ItemType&amp;。您已将 ItemType 定义为typedef char ItemType,因此insert 需要const char&amp;。如您所知,const char*指针)与const char&amp;引用)不同。您必须更改 insert 的声明和定义,或者将“Turkey”作为引用而不是指针传递。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-30
      • 2020-07-14
      • 2016-01-20
      • 2014-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多