我一直收到错误:从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;
}
答案 0 :(得分:0)
Class LinkedList有这个方法:
bool insert(int newPosition, const ItemType& newEntry);
在main.cpp
中你以这种方式调用这个方法:
groceryList->insert(1, "Turkey");
现在,“Turkey”的类型为const char*
,而方法insert
要求第二个参数为const ItemType&
类型。您已将ItemType定义为typedef char ItemType
,因此有效insert
需要const char&
。如您所知const char*
(指针)与const char&
(参考)不同。您必须更改insert
的声明和定义,或者传递“土耳其”作为参考,而不是作为指针。